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 + "\".");
}
}
}
問題是當我輸入一個句子沒有?要麼 !沒有打印。我的其他語句在其他樹如果沒有打印
請注意,如果在一行中有兩個「if」,並且沒有括號,那麼'else'將始終是最後一個if的else。也許這是問題所在。嘗試修復縮進和/或添加括號以查看「if-else-tree」的真實結構。另外,嘗試在第一個if條件中用'&&'替換同一行中的第二個'if'。 –
你應該更好地縮進你的代碼。並且尊重代碼約定。例如類名始終是首字母大寫。 – Carlo
那是那裏的邏輯混亂之一。 –