如果/ else语句,则每次都打印

时间:2017-03-26 16:33:00

标签: java if-statement

我正在编写代码,每次运行代码时,else语句总是使用if语句打印。我看了很多,但我无法弄清楚错误是什么。任何帮助,将不胜感激。谢谢!这是代码

import java.util.Scanner;

public class TextMsgAbbreviation {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      String userText = "";

      System.out.println("Input an abbreviation: ");
      userText = scnr.nextLine();

      if (userText.equals("LOL")) {
         System.out.println("laughing out loud");
      }

      if (userText.equals("IDK")) {
         System.out.println("I don't know");
      }

      if (userText.equals("BFF")) {
         System.out.println("best friends forever");
      }

      if (userText.equals("IMHO")) {
         System.out.println("in my humble opinion");
      }

      if (userText.equals("TMI")) {
         System.out.println("too much information");
      }

      else {
         System.out.println("Unknown");
      }

      return;
   }
}

5 个答案:

答案 0 :(得分:1)

正在检查每个if语句,如果不是userText.equals("TMI"),则它会始终打印Unknown

解决方案是放置else if,因此只有当if的条件为假时,它才会检查其他if

import java.util.Scanner;

public class TextMsgAbbreviation {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      String userText = "";

      System.out.println("Input an abbreviation: ");
      userText = scnr.nextLine();

      if (userText.equals("LOL")) {
         System.out.println("laughing out loud");
      }else if (userText.equals("IDK")) {
         System.out.println("I don't know");
      }else if (userText.equals("BFF")) {
         System.out.println("best friends forever");
      }else if (userText.equals("IMHO")) {
         System.out.println("in my humble opinion");
      }else if (userText.equals("TMI")) {
         System.out.println("too much information");
      }else {
         System.out.println("Unknown");
      }

      return;
   }
}

答案 1 :(得分:0)

仔细查看代码的这一部分,例如:

  if (userText.equals("IMHO")) {
     System.out.println("in my humble opinion");
  }

  if (userText.equals("TMI")) {
     System.out.println("too much information");
  }

  else {
     System.out.println("Unknown");
  }

让我们假设你的userText是" TEST"

first if语句为false,second也为false;然后输入else

需要通过if else而不是if if链接

答案 2 :(得分:0)

else语句将执行,因为只链接到最后一个if。如果最后一个if为false(输入不是“TMI”),它将执行。 if if if if(除了第一个,offcourse)

import java.util.Scanner;

public class TextMsgAbbreviation {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText = "";

System.out.println("Input an abbreviation: ");
userText = scnr.nextLine();

if (userText.equals("LOL")) 
     System.out.println("laughing out loud");

else if (userText.equals("IDK")) 
     System.out.println("I don't know");

else if (userText.equals("BFF")) 
     System.out.println("best friends forever");

else if (userText.equals("IMHO")) 
     System.out.println("in my humble opinion");

else if (userText.equals("TMI")) 
     System.out.println("too much information");

else 
     System.out.println("Unknown");

  return;

} }

答案 3 :(得分:0)

您只是将else应用于上一个if语句。如果要修复它,可以在初始化后的每个后续if语句中使用else if。使用switch语句可能是一种潜在的解决方案,但请参阅下面的内容,以获得更高效的能够更具扩展性的内容。

或者,您可以尝试一种可以改善圈复杂度的解决方案。如果您使用地图,则可以使用getOrDefault方法执行您对if else组合所执行的操作。

请参阅Cyclomatic complexity

见下文: -

final Map<String,String> foo = new HashMap<>();
foo.put ("LOL", "Laughing out loud");
foo.put ("IDK", "I don't know");
foo.put ("BFF", "best friends forever");
// and so on for the other values

然后,您可以使用以下内容代替if/else

final String value = foo.getOrDefault(userText, "Unknown");
System.out.println (value);

其他情况将是密钥不在地图中的默认情况。

我认为这是一个更好的解决方案的原因是因为你基本上创建了一个字典,而地图最适合这个而不是一系列if语句。

答案 4 :(得分:0)

使用开关,它可以根据用户输入的字符串简单地知道输出哪个

public static void main(String[] args)
        {
            Scanner scnr = new Scanner(System.in);
          String userText = "";

          System.out.println("Input an abbreviation: ");
          userText = scnr.nextLine();
          //test the userText at switch structure
         switch(userText)
         {
                     //if LOL is type by the User
             case "LOL":
                System.out.println("laughing out loud");
                 break;
        //if IDK is type by the User     
             case "IDK":
             System.out.println("I don't know");
                 break;
                     //if BFF is type by the User
             case "BFF":
             System.out.println("best friends forever");
                 break;
             //if IMHO is type by the User
             case "IMHO":
             System.out.println("in my humble opinion");
                 break;
             //if TMI is type by the User
             case "TMI": 
             System.out.println("too much information");
                 break;
             //if any other entered String
             default:
             System.out.println("Unknown");
             break;


         }


        }