0

感谢您花时间帮助我。我需要这个 switch 语句只接受整数 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)

;

4

3 回答 3

3

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

    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);
于 2013-07-27T00:28:35.657 回答
1

您可以设置“默认”大小写。

default: doSomething();
                 break;

当用户输入的值不是您的情况之一时,这将被调用。

} while (answer != 5);

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

编辑:

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

switch(answer) {
于 2013-07-27T00:34:40.853 回答
0

你不需要把它放在一个循环中。用例很简单:

  • 对于 1-4 :做某事然后返回
  • 对于 5:退出/归还一切
  • else: 再次请求输入

    公共静态无效主要(字符串[]参数){

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

    }

于 2013-07-27T04:57:36.840 回答