切换语句+用户输入

时间:2013-07-27 00:24:12

标签: java

感谢您抽出宝贵时间帮助我。我需要这个switch语句只接受ints 1-4。输入的任何其他人将再次要求输入。输入5将退出系统。

     System.out.println("A random numbers list has been generated for you:\n ");

     System.out.println("Choose an option:\n1)Form list to be heapified.\n2)Enqueue the integer 10" +
        "\n3)Dequeue the integer 10.\n4)Print the updated heap.\n5)Quit the system \n>>"); 
     Scanner scanner = new Scanner( System.in );
     int var = 0;
     String input = scanner.next();
     int answer = Integer.parseInt(input);

     do{
        input = scanner.next();
        answer = Integer.parseInt(input);
        var = answer;

     switch(var){
        case 1:
           for (int i = 0; i < 20; i++) {
              h.insert(new Integer((int)(100 * Math.random())), i);
           }
           break;
        case 2:
           System.out.println("\nEnqueue-ing 10...\n");
                pushFoward(10, 20);//priority 20
           break;
        case 3:
        System.out.println("\nDequeue-ing 10...\n");
            dequeue;//priority highest deleted

           break;
        case 4:
           while (h.heapsize() > 0) {
              System.out.print(h.pop() + " ");
           } 
           break;
     } 
     }while(var ==1 || var==2 || var==3
     || var==4);

我似乎无法正确行事。继续使情况变得更糟。

编辑:

 do{
    String input = scanner.next();
             int answer = Integer.parseInt(input);
     switch(var){
        case 1:
           for (int i = 0; i < 20; i++) {
              h.insert(new Integer((int)(100 * Math.random())), i);
           }
           break;
        case 2:
           System.out.println("\nEnqueue-ing 10...\n");
                h.pushFoward(10, 20);//priority 20
           break;
        case 3:
        System.out.println("\nDequeue-ing 10...\n");
            h.dequeue();//priority highest deleted

           break;
        case 4:
           while (h.heapsize() > 0) {
              System.out.print(h.pop() + " ");
           } 
           break;
           default: input = scanner.next();
            break;
     } 
     }while(var!=5)

3 个答案:

答案 0 :(得分:3)

尝试添加“default:”语句,如下所示:

    switch(var){
    case 1:
       for (int i = 0; i < 20; i++) {
          h.insert(new Integer((int)(100 * Math.random())), i);
       }
       break;
    case 2:
       System.out.println("\nEnqueue-ing 10...\n");
            pushFoward(10, 20);//priority 20
       break;
    case 3:
    System.out.println("\nDequeue-ing 10...\n");
        dequeue;//priority highest deleted

       break;
    case 4:
       while (h.heapsize() > 0) {
          System.out.print(h.pop() + " ");
       } 
       break;
    default:
        *Add whatever code you want to execute if its greater then or equal to 5 here!*

 }while(var ==1 || var==2 || var==3
 || var==4);

答案 1 :(得分:1)

您可以设置“默认”案例。

default: doSomething();
                 break;

当用户输入的值不属于您的情况时,将调用此方法。

} while (answer != 5);

这应该在输入5时使循环中断。

修改

此外,您需要打开答案变量而不是'var'

switch(answer) {

答案 2 :(得分:0)

您无需将其置于循环中。用例很简单:

  • 1-4:做某事然后返回
  • For 5:quit / return Everything
  • 否则:再次要求输入

    public static void main(String [] args){

    Scanner scanner = new Scanner(System.in);
    int answer = Integer.parseInt(scanner.next());
    
      switch(answer) {
        case 1:
            System.out.println(1);
            break;
        case 2:
            System.out.println(2);
            break;
        case 3:
            System.out.println(3);
            break;
        case 4:
            System.out.println(4);
            break;
        case 5:
            return; // System.exit(0) or quit however you want to
        default:
            answer = Integer.parseInt(scanner.next());
        }
    

    }