java 从终端读取 yes/no 布尔值并生成带有输入值的 if else

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10386011/
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-10-31 00:48:56  来源:igfitidea点击:

Read in yes/no boolean from terminal and produce an if else with inputted value

java

提问by Javaer

I asked a user if they would like to play a game..

我问用户他们是否想玩游戏..

    System.out.println("Would you like to play"); 
     read in yes or no value 
        if yes (display text) 
        else (display other text)

I have used the scanner previously in the program, I just need it to use it again. Do I need to declare another new one with new variables?

我之前在程序中使用过扫描仪,我只需要再次使用它。我需要用新变量声明另一个新变量吗?

回答by 11684

you could do something like:

你可以这样做:

    String yesOrNo = System.in.readLine();
    String textToDisplay = (yesOrNo.equals("Yes")) ? "text to display when yesOrNo equals yes" : "text to display when YesOrNo equals no";

回答by Chad

Use this method:

使用这个方法:

scanner.nextBoolean()

So then it'd be:

那么那就是:

System.out.println("Would you like to play?");
Scanner scanner = new Scanner(System.in);

if(scanner.nextBoolean()==true) {
    System.out.println("This will be fun");
} else {
    System.out.println("Maybe next time");
}


Edit

编辑



System.out.println("Would you like to play?");
Scanner scanner = new Scanner(System.in);
String val = scanner.next();
if(val.equalsIgnoreCase("y")||val.equalsIgnoreCase("yes")) {
    System.out.println("This will be fun");
} else if(val.equalsIgnoreCase("n")||val.equalsIgnoreCase("no")) {
    System.out.println("Maybe next time");
} else { 
System.out.println("Invalid character");

回答by dragon66

To make it simple, here is something for you to play with:

为了简单起见,这里有一些东西供您玩:

import java.util.Scanner;

public class ScannerExample {

  public static void main(String[] args) {

    System.out.println("Would you like to play: 'y' or 'yes' to accept; 'n' or 'no' to reject; 'q' to quit:");
    Scanner scanner = new Scanner(System.in);    
    String token = "";

    while(scanner.hasNextLine())
    {
       token = scanner.nextLine().trim();

       if(token.equalsIgnoreCase("q")) System.exit(0);

       if(token.equalsIgnoreCase("y")||token.equalsIgnoreCase("yes")) 
       {
           System.out.println("Thanks for your interest!");
           System.exit(0);
       }
       else if (token.equalsIgnoreCase("n")||token.equalsIgnoreCase("no"))
       {
           System.out.println("That's a pity!");
           System.exit(0);
       }
       else
       {
           System.out.println("Oops, not a valid input!");
       }
    }
  }
}

回答by ctitze

I just stumbled upon this question and the answer of Chad M is correct, but - like some commenters mentioned above - has some bugs in it. So here is my implementation without the bugs (it was just a question of saving the user input into a string variable):

我只是偶然发现了这个问题,Chad M 的回答是正确的,但是 - 就像上面提到的一些评论者 - 有一些错误。所以这是我没有错误的实现(这只是将用户输入保存到字符串变量的问题):

Scanner scanner = new Scanner(System.in);
String userInput = scanner.next();

if(userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("yes")) {
    // y or yes
} else {
    // other character
}

回答by Chad

Based on a recommendation of dragon66, I'm implementing a String comparison of predefined inputs compared to user input.

根据dragon66 的推荐,我正在实现预定义输入与用户输入的字符串比较。

System.out.println("Would you like to play? Or press Q to exit"):
    Scanner scanner = new Scanner(System.in);
    String affirmative = "yes";
    String affirmative2 = "y";
    String negative = "no";
    String negative2 = "n";
    String quit = "q";

if(scanner.next().equalsIgnoreCase(affirmative)||scanner.next().equalsIgnoreCase(affirmative2)) {
    System.out.println("This will be fun");
} else if(scanner.next().equalsIgnoreCase(negative)||scanner.next().equalsIgnoreCase(negative2)) {
    System.out.println("Maybe next time");
} else if(scanner.next().equalsIgnoreCase(quit)){ 
    System.out.println("Good bye");
    System.exit();
} else {
System.out.println("Invalid character");
}