在Java中提示用户是或否输入

时间:2014-04-13 19:20:39

标签: java variables loops

如何提示用户循环代码yes是循环no退出和错误输入打印错误输入并返回语句即。 “你想输入另一个名字:”

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;
        }
    }
    }
}

5 个答案:

答案 0 :(得分:3)

  1. 如果您不使用Java 7,则无法使用switch-strings
  2. 使用while (true)更改while (yn),以便在键入" no"并将boolean yn;更改为boolean yn = true;时停止 并且也改变了案例中的规则。

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

    yn = true; if" yes&#34 ;;

    yn = false; if" no";

    您可以使用while (!yn)更改while内的条件,但如果是,则更直观地允许yn true; false如果没有。

  3. return default;没有多大意义,如果你想让用户在错误的情况下重复...你应该重新做一个while (true)重复,直到他写了一个纠正一个。我会写另一种方法。

  4. 你可以这样做

    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 = repeat = true;
                            break;
    
                        case "no":
                            yn = repeat = false;
                            break;
                    }
                }
                break;
        }
    }
    

    是的,它会重复decision代码,但它是如何创建的,我认为这是唯一的方法。

答案 1 :(得分:2)

是的,但您需要while(yn),而不是while(true),这将永远持续下去。 break只会从switch语句中断。


好的,试试这个:

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)...");
      }
    }
  }
}

答案 2 :(得分:1)

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
}

尝试这样做

答案 3 :(得分:1)

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

答案 4 :(得分:0)

我刚刚创建了一个小类 YesNoCommandPrompt ,它就是这样做的:

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();
            }
        }
    }
}