在为我的程序编写代码时,我想在继续编写操作之前测试第一部分。虽然我有用户输入,但我希望在每次操作(add,deltete ..)之后显示选项,直到用户按下exit。如何修改我的代码呢?
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 );
}
}
答案 0 :(得分:2)
只需将您的while条件更改为:
} while ( choice > 0 && choice < 5 );
答案 1 :(得分:1)
虽然我有用户输入,但我希望显示选项 每次操作(添加,删除..)完成后直到用户按下 退出。强>
您可以设置int flag=0;
,当用户选择退出选项设置标记为1时,告诉循环退出。截至目前,您已经突破> 5
或< 1
{ {1}}因此无需在default case
中添加该条件。
while
严肃编辑
作为Java程序员,我可能建议您使用int flag=0;//Declare outside the loop
do{
...
case 5: System.out.println("Out we go.");
flag=1;//Set flag to 1 if user enters 5
break;
...
} while ( flag!=1 );//Exit the loop when flag==1
//Or directly while ( choice!=5 );
基元类型进行标记。
boolean
还有一件事:
使用boolean flag=true;//Declare outside the loop
do{
...
case 5: System.out.println("Out we go.");
flag=false;//Set flag to 1 if user enters 5
break;
...
} while (flag);//Exit the loop when flag==false
环绕代码以省略无效输入并再次提示输入。
大多数情况下,不建议您吞下try-catch
。
Exception
答案 2 :(得分:1)
首先确保您的扫描仪确实有一个int,使用sc.hasNextInt()
来验证用户输入的数字。要结束“5.Exit”处的do / while循环,只需使用do{...}while(choice!=5)
即可。以下代码未经过测试。
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 5.Exit");
System.out.println("Enter your Choice : ");
choice = -1;
Scanner sc = new Scanner(System.in);
// validate the next thing in your scanner is an int
// otherwise, sc.nextInt() might cause an exception
if (sc.hasNextInt()){
choice = sc.nextInt();
switch (choice) {
case 1: System.out.println("Getting ready to Add a Record ");
// ...
break;
case 2: System.out.println("Getting ready to Delete a Record ");
// ...
break;
case 3: System.out.println("Getting ready to Update a Record ");
// ...
break;
case 4: System.out.println("Here is your record ");
// ...
break;
case 5: System.out.println("Out we go.");
// skip System.exit(0), your main method ends
// automatically when you leave your do/while loop
break;
default: System.out.println("Try again");
break;
}
}
// if choice == 5 it ends, otherwise, starts over...
} while ( choice != 5 );
}
}
答案 3 :(得分:-1)
只需将你的while循环设为:
} while ( choice!=0 );(The Wrong one)
校正:
} while(choice!=0 && choice<=5)