Java输出是否,否则?

时间:2018-11-18 20:48:18

标签: java if-statement

我想按字母顺序排列我的琴弦。我的代码也可以。但是,当我输入汉斯(Hans),安娜(Anna)和麦克斯(Max)字符串时,他同时给了我“ if”和“ else” println。错误在哪里?

import java.util.Scanner;

class ersteAufgabe {

public static void main(String args[]) {

          Scanner scan = new Scanner(System.in);
          String s1 = scan.nextLine();
          String s2 = scan.nextLine();
          String s3 = scan.nextLine();
          if (s1.compareTo(s2) < 0 & s1.compareTo(s3) < 0 & s2.compareTo(s1) > 0 & s2.compareTo(s3) < 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) > 0) 
          {
            System.out.println("Ordnung:" + s1 +" "+ s2 +" " + s3);  
          }
          if (s1.compareTo(s2) < 0 & s1.compareTo(s3) < 0 & s2.compareTo(s1) > 0 & s2.compareTo(s3) > 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) < 0) 
          {
            System.out.println("Ordnung:" + s1 +" "+ s3 +" " + s2);  
          }
          if (s1.compareTo(s2) > 0 & s1.compareTo(s3) < 0 & s2.compareTo(s3) < 0 & s2.compareTo(s1) < 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) > 0) 
          {
            System.out.println("Ordnung:" + s2 +" "+ s1 +" " + s3);  
          }
          if (s1.compareTo(s2) > 0 & s1.compareTo(s3) > 0 & s2.compareTo(s1) < 0 & s2.compareTo(s3) < 0 & s3.compareTo(s1) < 0 & s3.compareTo(s2) > 0) 
          {
            System.out.println("Ordnung:" + s2 +" "+ s3 +" " + s1);  
          }
          if (s1.compareTo(s2) < 0 & s1.compareTo(s3) > 0 & s2.compareTo(s3) > 0 & s2.compareTo(s1) > 0 & s3.compareTo(s1) < 0 & s3.compareTo(s2) < 0)
          {
            System.out.println("Ordnung:" + s3 +" "+ s1 +" " + s2);  
          }
          if (s1.compareTo(s2) > 0 & s1.compareTo(s3) > 0 & s2.compareTo(s1) < 0 & s2.compareTo(s3) > 0 & s3.compareTo(s2) < 0 & s3.compareTo(s1) < 0)
          {
            System.out.println("Ordnung:" + s3 +" "+ s2 +" " + s1);  
          } else{
              System.out.println("Ungueltige Eingabe");
          }

    }
 }

3 个答案:

答案 0 :(得分:3)

您代码中的else仅连接到最后一个if。这意味着,如果其他if中的一个为true(而最后一个if为false),则也将执行else。所以试试这个:

public static void main(String args[]) {

          Scanner scan = new Scanner(System.in);
          String s1 = scan.nextLine();
          String s2 = scan.nextLine();
          String s3 = scan.nextLine();
          if (s1.compareTo(s2) < 0 & s1.compareTo(s3) < 0 & s2.compareTo(s1) > 0 & s2.compareTo(s3) < 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) > 0) 
          {
            System.out.println("Ordnung:" + s1 +" "+ s2 +" " + s3);  
          }
          else if (s1.compareTo(s2) < 0 & s1.compareTo(s3) < 0 & s2.compareTo(s1) > 0 & s2.compareTo(s3) > 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) < 0) 
          {
            System.out.println("Ordnung:" + s1 +" "+ s3 +" " + s2);  
          }
          else if (s1.compareTo(s2) > 0 & s1.compareTo(s3) < 0 & s2.compareTo(s3) < 0 & s2.compareTo(s1) < 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) > 0) 
          {
            System.out.println("Ordnung:" + s2 +" "+ s1 +" " + s3);  
          }
          else if (s1.compareTo(s2) > 0 & s1.compareTo(s3) > 0 & s2.compareTo(s1) < 0 & s2.compareTo(s3) < 0 & s3.compareTo(s1) < 0 & s3.compareTo(s2) > 0) 
          {
            System.out.println("Ordnung:" + s2 +" "+ s3 +" " + s1);  
          }
          else if (s1.compareTo(s2) < 0 & s1.compareTo(s3) > 0 & s2.compareTo(s3) > 0 & s2.compareTo(s1) > 0 & s3.compareTo(s1) < 0 & s3.compareTo(s2) < 0)
          {
            System.out.println("Ordnung:" + s3 +" "+ s1 +" " + s2);  
          }
          else if (s1.compareTo(s2) > 0 & s1.compareTo(s3) > 0 & s2.compareTo(s1) < 0 & s2.compareTo(s3) > 0 & s3.compareTo(s2) < 0 & s3.compareTo(s1) < 0)
          {
            System.out.println("Ordnung:" + s3 +" "+ s2 +" " + s1);  
          } else{
              System.out.println("Ungueltige Eingabe");
          }
    }
 }

答案 1 :(得分:2)

您的代码是一系列if,除了您的else所附的最后一个代码之外,它们都是独立的。如果您希望最后一个else充当整个链的else,则需要将中间的if转换为if else

if (condition1) {
    // Do something...
} else if (condition2) {
    // Do something else...
} else {
    // Do something if neither condition1 nor condition2
}

旁注:我看到您使用的是按位和运算符(&),而不是布尔值和运算符(&&)。尽管它可能适合您的情况,但我强烈警告您不要将按位运算符用于除按位操纵以外的任何操作,因为它的优先级高于不等式,而布尔运算符的优先级较低,这可能会导致意外结果。

答案 2 :(得分:0)

您需要将控制语句更改为

 if (s1.compareTo(s2) < 0 & s1.compareTo(s3) < 0 & s2.compareTo(s1) > 0 & s2.compareTo(s3) < 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) > 0) {
            System.out.println("Ordnung:" + s1 + " " + s2 + " " + s3);
        }
        else if (s1.compareTo(s2) < 0 & s1.compareTo(s3) < 0 & s2.compareTo(s1) > 0 & s2.compareTo(s3) > 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) < 0) {
            System.out.println("Ordnung:" + s1 + " " + s3 + " " + s2);
        }
        else if (s1.compareTo(s2) > 0 & s1.compareTo(s3) < 0 & s2.compareTo(s3) < 0 & s2.compareTo(s1) < 0 & s3.compareTo(s1) > 0 & s3.compareTo(s2) > 0) {
            System.out.println("Ordnung:" + s2 + " " + s1 + " " + s3);
        }
        else if (s1.compareTo(s2) > 0 & s1.compareTo(s3) > 0 & s2.compareTo(s1) < 0 & s2.compareTo(s3) < 0 & s3.compareTo(s1) < 0 & s3.compareTo(s2) > 0) {
            System.out.println("Ordnung:" + s2 + " " + s3 + " " + s1);
        }
        else if (s1.compareTo(s2) < 0 & s1.compareTo(s3) > 0 & s2.compareTo(s3) > 0 & s2.compareTo(s1) > 0 & s3.compareTo(s1) < 0 & s3.compareTo(s2) < 0) {
            System.out.println("Ordnung:" + s3 + " " + s1 + " " + s2);
        }
        else if (s1.compareTo(s2) > 0 & s1.compareTo(s3) > 0 & s2.compareTo(s1) < 0 & s2.compareTo(s3) > 0 & s3.compareTo(s2) < 0 & s3.compareTo(s1) < 0) {
            System.out.println("Ordnung:" + s3 + " " + s2 + " " + s1);
        } else {
            System.out.println("Ungueltige Eingabe");
        }

因此只有其中之一被执行。您当前拥有的是多个不相关的if语句!