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
那是那里的逻辑混乱之一。 –