用Java在Eclipse中做一个程序。我想要做的是当我执行程序时,我希望向用户提供一个选择。我已经完成了所有计算等工作,我只是不确定如何制作此菜单以提供用户选择。我正在寻找的示例:
To enter an original number: Press 1
To encrypt a number: Press 2
To decrypt a number: Press 3
To quit: Press 4
Enter choice:
Run Code Online (Sandbox Code Playgroud)
public static void main(String[] args) {
Data data = new Data();
data.menu(); }
}
Run Code Online (Sandbox Code Playgroud)
小智 9
为简单起见,我建议使用返回选项整数值的静态方法。
public static int menu() {
int selection;
Scanner input = new Scanner(System.in);
/***************************************************/
System.out.println("Choose from these choices");
System.out.println("-------------------------\n");
System.out.println("1 - Enter an original number");
System.out.println("2 - Encrypt a number");
System.out.println("3 - Decrypt a number");
System.out.println("4 - Quit");
selection = input.nextInt();
return selection;
}
Run Code Online (Sandbox Code Playgroud)
完成方法后,您将在主方法中相应地显示它,如下所示:
public static void main(String[] args) {
int userChoice;
/*********************************************************/
userChoice = menu();
//from here you can either use a switch statement on the userchoice
//or you use a while loop (while userChoice != the fourth selection)
//using if/else statements to do your actually functions for your choices.
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
74841 次 |
| 最近记录: |