Java If/else语句,else每次都在打印
我正在编写代码,每次运行它时,else语句总是与if语句一起打印。我已经看了很多遍了,不知道出了什么问题。任何帮助都将不胜感激。谢谢这是密码Java If/else语句,else每次都在打印,java,if-statement,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 = "";
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;
}
}
它检查每个
if
语句,如果不是userText.equals(“TMI”)
,它将始终打印未知的
解决方案是如果
,则放入else,因此只有当该的条件为false时,它才会检查其他if
s:
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;
}
}
仔细查看代码的这一部分,例如:
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”
如果第一个
语句为false,则第二个
语句也为false;然后输入else
您需要通过if else
而不是if
链接,因为else语句将执行,因为它只链接到最后一个if。如果最后一个if为false(输入不是“TMI”),它将执行。
将ifs设置为else if(当然第一个除外)
}
}您仅将else
应用于最后一条if
语句。如果要修复它,可以对初始值之后的每个后续If语句使用else If
。使用switch语句可能是一个潜在的解决方案,但请参阅下文,了解更高效、更具可扩展性的内容
或者,您可以尝试一种改进圈复杂度的解决方案。如果使用映射,则可以使用getOrDefault
方法来处理If-else
组合
看
见下文:-
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
else情况将是密钥不在映射中的默认情况
我认为这是一个更好的解决方案,因为您基本上是在创建一个字典,而映射最适合于此,而不是一系列if语句。使用switch,它可以根据用户输入的字符串简单地知道输出哪个
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;
}
}
您需要else if
对于大多数块(不是第一个块),A将是一个不错的选择。我正在考虑尝试else if,谢谢您的帮助!如果它回答了你的问题,请不要忘记。一个带有两个单词的代码块是一个糟糕的答案。请解释你的代码是做什么的,以及它是如何回答这个问题的。这仍然不能解释你的代码是做什么的,以及它是如何回答这个问题的。如果他不知道开关结构的话,那就差不多了
final String value = foo.getOrDefault(userText, "Unknown");
System.out.println (value);
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;
}
}