Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java else if树中的我的else语句不是';t型印刷_Java - Fatal编程技术网

Java else if树中的我的else语句不是';t型印刷

Java else if树中的我的else语句不是';t型印刷,java,Java,问题是当我输入一个句子时没有一个?或没有打印任何内容。我已经格式化了您的代码并添加了括号,现在应该很清楚发生了什么: import java.util.Scanner; public class youalwaystwo { public static void main(String[] args) { String sentence; Scanner keyboard = new Scanner(System.in); System.out.println(

问题是当我输入一个句子时没有一个?或没有打印任何内容。

我已经格式化了您的代码并添加了括号,现在应该很清楚发生了什么:

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 + "\".");
    }
  }
}
i、 e.你有一个大的
if
,检查结尾处的
——如果没有,什么都不会发生


从中吸取教训始终使用
{}
(当您对Java有了更深入的了解后,这条规则也有例外,但现在始终)。

如果
语句缩进严重,那么很难看到发生了什么。让我们正确地缩进它,并添加
{
}
,以明确代码的结构

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
的表达式将计算为
false
,其他所有内容都将被跳过。

我认为您的
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语句梯形图的布局非常混乱造成的


只需格式化代码即可避免此类问题。许多最近的Java编辑器都有“格式源”特性,可以自动完成这项工作。我确信,如果它正确对齐,很容易看到任何问题。此处使用的确切编码样式并不重要,只需使用任意一种。

注意,一行中有两个
if
,如果没有括号,则
else
始终是最后一个if。也许这就是问题所在。尝试修正缩进和/或添加括号,以查看“if-else树”的真实结构。另外,尝试将同一行中的第二个
if
替换为第一个if条件中的
&
。您应该更好地缩进代码。同时也尊重代码约定。例如,类名的第一个字母总是大写。这是一个逻辑混乱的问题。为每个
if
else
块添加括号是正确的做法,但最后一个
else
块的位置总是有争议的。@Ɍ,这就是现在编译器会对它进行评估的地方…这正是我的观点。对你、我和IDE来说,这似乎是正确的,但OP可能有不同的逻辑。这就是为什么我说这是有争议的:)但你使用括号的答案是正确的@Ɍ.Ɉ同意,OP的意图当然是有争议的。完全同意Boris的观点——最好使用
{}
,因为它有很多好处(其中一个好处对其他人来说是可读的)。
  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 !
   }
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)