2014-09-19 90 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