Java 如何使用字符串参数(即用户输入的字符串)创建 if-else 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27112685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:50:19  来源:igfitidea点击:

How do I make a if-else statement with the string argument ( that is a string input from the user)?

javaloopsif-statementwhile-loopdo-while

提问by Jabir Al Fatah

I am writing a program that asks the user to input his name, address and phone number. When the data is entered the program shall print the data and ask the user to verify the data by entering YES or NO. This process shall be repeated until the user is satisfied and answers YES to the question.

我正在编写一个程序,要求用户输入他的姓名、地址和电话号码。当输入数据时,程序将打印数据并要求用户通过输入 YES 或 NO 来验证数据。该过程应重复进行,直到用户满意并对问题回答“是”为止。

Now in my case I may have put a if-else statement inside the while loop in an inappropriate way. That's why it's not working as it is expected to be. How can I solve this?

现在就我而言,我可能以不适当的方式在 while 循环中放置了一个 if-else 语句。这就是为什么它没有按预期工作的原因。我该如何解决这个问题?

Also I only have tried the promt asking to enter the users name. But if I want to add more prompt with different question in the similiar way then how could I do that?

另外我只试过要求输入用户名的提示。但是,如果我想以类似的方式添加更多带有不同问题的提示,那么我该怎么做呢?

Code:

代码:

package userinfo;    
import java.util.Scanner;

public class UserInfo {

    public static void main(String[] args) {    
        String name;
        String yes = "YES";
        String no = "NO";    
        Scanner userInput = new Scanner(System.in);

        System.out.println("Enter your name:");
        name = userInput.next();
        System.out.println("Please varify your name by typing YES or NO");

        while (true) {
            String input = userInput.next();
            if (input == yes) {
                System.out.println("Your name is: " + name);
            }
            if (input == no) {
                System.out.println("Enter your name again");

            } else {
                System.out.println("Invalid input! Enter value again:");
                break;
            }    
        }    
    }    
}

采纳答案by Sarthak Mittal

You should use it like this:

你应该像这样使用它:

if (input.equals(yes)) {
  System.out.println("Your name is: " + name);
}
else if (input.equals(no)) {
 System.out.println("Enter your name again");
}

Because the statement:

因为声明:

input==yes;

only checks if the references are equal or not

只检查引用是否相等

And to input more values from the user you can do it like this:

要从用户输入更多值,您可以这样做:

    System.out.println("Enter your name:");     //This you already did
    name = userInput.next
    System.out.println("Enter your surname:");
    String surname = userInput.next();

full code:(Asking for multiple prompts)

完整代码:(要求多提示)

package userinfo;

import java.util.Scanner;

public class UserInfo {

public static void main(String[] args) {

    String name;
    int teleNum;
    String inputTeleNum;
    String yes = "YES";
    String no = "NO";

    Scanner userInput = new Scanner(System.in);

    while (true) {

        System.out.println("Enter your name:");
        name = userInput.next();

        System.out.println("Please varify your name by typing YES or NO");

        String input = userInput.next();

        if (input.equals(yes)) {
            System.out.println("Your name is: " + name);
        } else if (input.equals(no)) {
            System.out.println("Enter your name again");

        }

        while (true) {

            System.out.println("Enter your telephone number:");
            teleNum = userInput.nextInt();

            System.out.println("Please varify your telephone number by typing YES or NO");

            inputTeleNum = userInput.next();

            if (inputTeleNum.equals(yes)) {
                System.out.println("Your telephone num is: " + teleNum);
            } else if (inputTeleNum.equals(no)) {
                System.out.println("Enter your tele number again: ");

            }

        }

    }

}

}

回答by vembutech

To compare String use the methods equals()or equalsIgnoreCase(). this will resolve your issue.

要比较字符串,请使用方法equals()equalsIgnoreCase()。这将解决您的问题。

回答by Juan de la hoz

The breakstatement should go inside

break声明应往里走

if(input.equals(yes)) 

Not in the else{}.

不在else{}.

回答by cringe

Since JDK 7 it's possible to use a Stringin a switchstatement:

从 JDK 7 开始,可以语句中使用 aStringswitch

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
     String typeOfDay;
     switch (dayOfWeekArg) {
         case "Monday":
             typeOfDay = "Start of work week";
             break;
         case "Tuesday":
         case "Wednesday":
         case "Thursday":
             typeOfDay = "Midweek";
             break;
         case "Friday":
             typeOfDay = "End of work week";
             break;
         case "Saturday":
         case "Sunday":
             typeOfDay = "Weekend";
             break;
         default:
             throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
     }
     return typeOfDay;
}

Maybe this is applicable in your situation. You loose the options of #equalsIgnoreCase()though.

也许这适用于您的情况。不过,您失去了#equalsIgnoreCase()的选项。