2014-02-06 38 views
-3
import java.util.Scanner; 

public class youalwaystwo 
{ 
    public static void main(String[] args) 
    { 
    String sentence; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Please enter a sentence."); 
    sentence = keyboard.nextLine(); 
    { 
    if (sentence.charAt(sentence.length()-1) == '?') if (sentence.length() %2 == 0) 
      System.out.println("Yes."); 
     else if (sentence.charAt(sentence.length()-1) == '?') if (sentence.length() %1 == 0) 
      System.out.println("No."); 
     else if (sentence.charAt(sentence.length()-1) == '!') 
      System.out.println("Wow."); 
     else 
      System.out.println("You always say \"" + sentence + "\"."); 
    } 
    } 
} 

問題是當我輸入一個句子沒有?要麼 !沒有打印。我的其他語句在其他樹如果沒有打印

+1

請注意,如果在一行中有兩個「if」,並且沒有括號,那麼'else'將始終是最後一個if的else。也許這是問題所在。嘗試修復縮進和/或添加括號以查看「if-else-tree」的真實結構。另外,嘗試在第一個if條件中用'&&'替換同一行中的第二個'if'。 –

+3

你應該更好地縮進你的代碼。並且尊重代碼約定。例如類名始終是首字母大寫。 – Carlo

+1

那是那裏的邏輯混亂之一。 –

回答

4

我已經格式化您的代碼,增加了支架,現在應該是相當明顯發生了什麼:

public static void main(String[] args) { 
    String sentence; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Please enter a sentence."); 
    sentence = keyboard.nextLine(); 
    if (sentence.charAt(sentence.length() - 1) == '?') { 
     if (sentence.length() % 2 == 0) { 
      System.out.println("Yes."); 
     } else if (sentence.charAt(sentence.length() - 1) == '?') { 
      if (sentence.length() % 1 == 0) { 
       System.out.println("No."); 
      } else if (sentence.charAt(sentence.length() - 1) == '!') { 
       System.out.println("Wow."); 
      } else { 
       System.out.println("You always say \"" + sentence + "\"."); 
      } 
     } 
    } 
} 

即你有一個很大的if,在結束了?檢查 - 如果沒有沒有任何反應。

一個教訓,帶走這個; 總是使用{}(當你瞭解Java稍微好一些時,這條規則也有例外,但現在總是)。

+0

爲每個'if'和'else'塊添加括號是正確的做法,但最後一個'else'的位置總是有爭議的。 – SudoRahul

+0

@Ɍ.Ɉ - 這是我的IDE建議的,所以現在編譯器會評估它... –

+0

正是我的觀點。對你來說,我和IDE似乎是正確的,但OP可能有不同的邏輯。這就是爲什麼我說這是有爭議的:)但你的答案使用括號是正確的! – SudoRahul

1

你有一個糟糕的縮進if陳述,所以很難看到會發生什麼。讓我們正確縮進它並添加{},以清楚說明代碼的結構。

if (sentence.charAt(sentence.length()-1) == '?') { 
    if (sentence.length() %2 == 0) { 
     System.out.println("Yes."); 
    } 
    else if (sentence.charAt(sentence.length()-1) == '?') { 
     if (sentence.length() %1 == 0) { 
      System.out.println("No."); 
     } 
     else if (sentence.charAt(sentence.length()-1) == '!') { 
      System.out.println("Wow."); 
     } 
     else { 
      System.out.println("You always say \"" + sentence + "\"."); 
     } 
    } 
} 

現在你看看這句話沒有?末會發生什麼:第一個if的表達式會false一切將被跳過。

0

我覺得你if語句應該是這樣的:

if (sentence.charAt(sentence.length() - 1) == '?') {///// for ? 
    if (sentence.length() % 2 == 0) { 
     System.out.println("Yes."); 
     } else if (sentence.length() % 1 == 0) { 
      System.out.println("No."); 
     } 
    } else if (sentence.charAt(sentence.length() - 1) == '!') {/// for ! 
      System.out.println("Wow."); 
    } else { 
      System.out.println("You always say \"" + sentence + "\".");/// for anything else without ? or ! 
    } 
+0

這種格式比OP的好... –

0

變化

if (sentence.charAt(sentence.length()-1) == '?') if (sentence.length() %2 == 0) 
    System.out.println("Yes."); 
else if (sentence.charAt(sentence.length()-1) == '?') if (sentence.length() %1 == 0) 

if (sentence.charAt(sentence.length()-1) == '?' && sentence.length() %2 == 0) 
    System.out.println("Yes."); 
else if (sentence.charAt(sentence.length()-1) == '?' && sentence.length() %1 == 0) 
0

我認爲實際的問題已經造成非常混亂的佈局的if語句階梯。

只需對代碼進行格式化即可避免此類問題。許多最近的Java編輯器都具有可以自動執行此操作的「格式源」功能。我相信它是正確對齊的,很容易看到任何問題。使用的確切編碼風格在這裏並不重要,只需使用任何。