简体   繁体   中英

Prompt for user yes or no input in Java

how to prompt user to loop the code yes to loop no to exit and wrong input print wrong input and go back to statement ie. "do you want to enter another name:"

import java.util.Scanner;

public class loop {
public static void main(String[] args){
    Scanner kbd = new Scanner (System.in);
    String decision;
    boolean yn;
    while(true){

        System.out.println("please enter your name");
        String name = kbd.nextLine();

        System.out.println("you entered the name" + name );

        System.out.println("enter another name : yes or no");
        decision = kbd.nextLine();

        switch(decision){
        case "yes":
            yn = false;
            break;
        case "no": 
            yn = true;
            break;
        default : 
            System.out.println("please enter again ");
             return default;
        }
    }
    }
}
  1. If you don't use Java 7 you can't use switch-strings
  2. Change while (true) with while (yn) so it will stop when he type "no", and change boolean yn; to boolean yn = true; And change the rules inside the cases too.

     case "yes": yn = false; break; case "no": yn = true; break;

    yn = true; if "yes" ;

    yn = false; if "no" ;

    You could change the condition inside the while, with while (!yn) but is more intuitive to let yn true if yes; false if no.

  3. return default; don't make much sense, if you want to let user repeat in case of error well.. you should do a new while (true) to repeat until he writes a correct one. I would write another method.

This is how you could do it

Scanner kbd = new Scanner (System.in);

String decision;

boolean yn = true;
while(yn)
{
    System.out.println("please enter your name");
    String name = kbd.nextLine();

    System.out.println("you entered the name" + name );

    System.out.println("enter another name : yes or no");
    decision = kbd.nextLine();


    switch(decision)
    {
        case "yes":
            yn = true;
            break;

        case "no":
            yn = false;
            break;

        default:
            System.out.println("please enter again ");
            boolean repeat = true;

            while (repeat)
            {
                System.out.println("enter another name : yes or no");
                decision = kbd.nextLine();

                switch (decision)
                {
                    case "yes":
                        yn = true;
                        repeat = false;
                        break;

                    case "no":
                        yn = repeat = false;
                        break;
                    default:
                        repeat = true;
                }
            }
            break;
    }
}

Yes it will repeat decision code, but how it is created i think it is the only way to do it.

Yes, but you'd want while(yn) , not while(true) , which goes on forever. The break only breaks from the switch statement.


Alright, try this:

import java.util.Scanner;

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

  boolean checking = true, valid = true;
  String[] names = new String[50];
  int i = 0;

  while(checking){
    System.out.println("Enter name...");
    me = s.nextLine();
    System.out.println("You entered " + me + ".");
    while(valid){
      System.out.println("Enter another? y/n");
      you = s.nextLine();
      if(you.equals("n")){
        valid = false;
        checking = false;
      }else if you.equals("y")){
        names[i] = you;
        i++;
        valid = false;
      }else{
        System.out.println("Sorry, try again (y/n)...");
      }
    }
  }
}
boolean input = true;
while(input){
//ask for name
//print message saying you entered xyz
//ask if user wants to enter other name
//if user enters no
//set input = false;
//else continue
}

try doing this

而不是 break 和 while(true),您需要说while(!yn)因为当用户输入 yes 时将 yn 设置为 false。

I just created a small class YesNoCommandPrompt that does just that:

private String prompt;
private Supplier<T> yesAction;
private Supplier<T> noAction;

public YesNoCommandPrompt(String prompt, Supplier<T> yesAction, Supplier<T> noAction) {
    this.prompt = prompt;
    this.yesAction = yesAction;
    this.noAction = noAction;
}

// example usage
public static void main(String[] args) {
    YesNoCommandPrompt<String> prompt = new YesNoCommandPrompt<>(
            "Choose",
            () -> {return "yes";},
            () -> {return "no";});

    System.out.println(prompt.run());
}

public T run() {
    final String yesOption = "y";
    final String noOption = "n";
    try (Scanner scanner = new Scanner(System.in)) {
        while (true) {
            System.out.print(prompt + " [" + yesOption + "/" + noOption + "]: ");
            String option = scanner.next();
            if (yesOption.equalsIgnoreCase(option)){
                return yesAction.get();
            }
            else if (noOption.equalsIgnoreCase(option)){
                return noAction.get();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM