2014-09-19 86 views
0

我的代码风格有什么问题吗? 这是家庭作业,我试图提交,但它说我的风格是错误的:/编码风格有问题

import java.util.Scanner; 
public class Groucho { 
    String secret; 
    public Groucho(String secret) { 
     this.secret = secret; 
    } 
    public boolean saysSecret(String line) { 
     if (line.indexOf(secret) != -1) 
      return true; 
     else 
      return false; 
    } 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Enter your secret word here: "); 
     String a = in.nextLine(); 
     Groucho x = new Groucho(a); 
     System.out.println("Please enter a sentence until you win the game"); 
     while (in.hasNextLine()) { 
      String inp = in.nextLine(); 
      if (x.saysSecret(inp)) { 
       System.out.println("You have won $100 for saying " + a); 
       break; 
      } 
      else 
       System.out.println("Try again"); 
     } 
    } 
} 

这就是我的风格检查说是错误的:

Request 1 
File received, running checkstyle... 
Starting audit... 
Groucho.java:20: while child at indentation level 11 not at correct indentation, 12 
Groucho.java:21: if at indentation level 11 not at correct indentation, 12 
Groucho.java:22: if child at indentation level 15 not at correct indentation, 16 
Groucho.java:22: method call child at indentation level 15 not at correct indentation, 16 
Groucho.java:23: if child at indentation level 15 not at correct indentation, 16 
Groucho.java:24: if rcurly at indentation level 11 not at correct indentation, 12 
Groucho.java:25: else at indentation level 11 not at correct indentation, 12 
Groucho.java:26: method call child at indentation level 15 not at correct indentation, 16 
Audit done. 

Done! 
+0

一个挑剔:改变'saidSecret()'简单地说就是'return line.indexOf(secret)> = 0;'。 – user949300 2014-09-19 07:09:35

+0

谢谢:)没想到那 – Panthy 2014-09-19 07:10:50

+3

这个问题更适合https://codereview.stackexchange.com/。 – 2014-09-19 07:20:25

回答

0

所以显然我只需要在每个方法之间添加一个空格。愚蠢但是是的。

1

有一件事我喜欢do是放行换行以将相关代码分组成块或段落。我认为它使代码更易于阅读。至于你的错误消息,它看起来像你需要缩进你的线多一个空间?看起来它们比预期的要低一级。

+0

明白了.... nnnnnnnn – Panthy 2014-09-19 07:05:25

+0

它总是小事情。你的风格看起来不错,保持它! – jspitkin 2014-09-19 07:13:00

+0

我有一个问题......当我正在做一个类,我正在构造一个构造方法......我必须首先初始化构造方法之外的变量吗?像我在那里?字符串秘密; – Panthy 2014-09-19 07:14:12