在为我的程序编写代码时,我想在继续编写操作之前测试第一部分。虽然我有用户输入,但我希望在每个操作(添加、删除 ..)完成后显示选项,直到用户按下退出。我该如何修改我的代码来做到这一点?
import java.io.*;
import java.util.*;
public class Records {
public static void main(String[] args) {
int choice;
do {
System.out.println("1.Add \n 2.Delete \n 3.Update \n 4.Show \n Exit");
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//int choice;
System.out.println("Enter your Choice : ");
Scanner sc = new Scanner(System.in);
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Getting ready to Add a Record ");
//set();
break;
case 2:
System.out.println("Getting ready to Delete a Record ");
//delete();
break;
case 3:
System.out.println("Getting ready to Update a Record ");
//update();
break;
case 4:
System.out.println("Here is your record ");
//display();
break;
case 5:
System.out.println("Out we go.");
System.exit(0);
//exit();
break;
default:
System.out.println("Try again");
break;
}
} while ( choice > 5 || choice < 1 );
}
}
最佳答案
只需将您的 while 条件更改为:
} while ( choice > 0 && choice < 5 );
关于java - 如何让这个程序返回提示直到用户退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24795355/