重启switch case语句

时间:2015-04-27 05:33:15

标签: java menu switch-statement

希望我能在这里找到帮助,实际上我的设计程序让用户输入的字符串值包含0或1然后菜单出现帮助用户从4选项中选择我的问题我不能返回首次使用后显示的菜单因为拳头选择后程序无法运行任何选择... 再次感谢您

  Scanner input = new Scanner(System.in);
    System.out.print("Enter 0s or 1s Numbers  ");
    String binaryString = input.nextLine();// user must enter string value contains 0 or 1 
    convert.displayMenu();// call the display menu which contains 4 choices
    Scanner input2 = new Scanner(System.in);// the user select the 1 option from 4
    int select = input2.nextInt(); // to save what user enter it 
    // here is the switch statement im use it 
    switch (select) {
        case 1:
            input2.equals(1);
            convert.getBinary(binaryString);
            convert.displayMenu();
         break;
        case 2:
            input2.equals(2);
            convert.convertBtD(binaryString);
            convert.displayMenu();
            break;
        case 3:
            input2.equals(2);
            convert.convertBtO(binaryString);
            convert.displayMenu();
            break ;
        case 4:
            break;
    }
    while(select != 4);
    }

4 个答案:

答案 0 :(得分:2)

此循环重新启动switch / case语句:

inputloop: while(true) {
  int select = input2.nextInt();
  switch (select) {
    case 1:
        input2.equals(1);
        convert.getBinary(binaryString);
        break;
    case 2:
        input2.equals(2);
        convert.convertBtD(binaryString);
        break;
    case 3:
        input2.equals(2);
        convert.convertBtO(binaryString);
        break ;
    case 4:
        break inputloop;
  }
  convert.displayMenu();
}

这个代码可以用循环条件为select != 4的方式重写,但这个代码更简洁(从我的角度来看)

答案 1 :(得分:1)

boolean startLoop = true;
While(startLoop) {
switch(choice) {
case 1:
break;
case 2:
break;
case 3://exitChoice
startLoop = false;
break;

}

}

答案 2 :(得分:0)

你应该自己调用这个方法,如:

    public void menu(){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter 0s or 1s Numbers  ");
        String binaryString = input.nextLine();// user must enter string value contains 0 or 1 
        convert.displayMenu();// call the display menu which contains 4 choices
        Scanner input2 = new Scanner(System.in);// the user select the 1 option from 4
        int select = input2.nextInt(); // to save what user enter it 
        // here is the switch statement im use it  switch (select) {
            case 1:
                input2.equals(1);
                convert.getBinary(binaryString);
                convert.displayMenu();
             break;
            case 2:
                input2.equals(2);
                convert.convertBtD(binaryString);
                convert.displayMenu();
                break;
            case 3:
                input2.equals(2);
                convert.convertBtO(binaryString);
                convert.displayMenu();
                break ;
            case 4:
                break;

            case 5: 
                System.out.println("Exit Point");
                System.exit(0);
        } 
menu();
}

我只是将此代码添加到方法中,并在方法的末尾添加一个调用菜单()。我还创建了案例5以退出应用程序。这是你的逻辑如何做到正确,但你得到了主要的轮廓来实现这一点:)

答案 3 :(得分:0)

可以像这样实现一个简单的解决方案:

r.append({k: a[k] for k in ("content", "phrase", "name")})

}