Java 如何使该程序返回提示状态,直到用户退出
在为我的程序编写代码时,我考虑在继续编写操作之前测试第一部分。虽然我有用户输入,但我希望在每次操作(添加、删除…)完成后显示选项,直到用户按退出。如何修改我的代码来实现它Java 如何使该程序返回提示状态,直到用户退出,java,Java,在为我的程序编写代码时,我考虑在继续编写操作之前测试第一部分。虽然我有用户输入,但我希望在每次操作(添加、删除…)完成后显示选项,直到用户按退出。如何修改我的代码来实现它 import java.io.*; import java.util.*; public class Records { public static void main(String[] args) { int choice; do { System.out.
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 );
}
}
import java.io.*;
导入java.util.*;
公开课堂记录{
公共静态void main(字符串[]args){
智力选择;
做{
System.out.println(“1.Add\n2.Delete\n3.Update\n4.Show\n Exit”);
//BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
//智力选择;
System.out.println(“输入您的选择:”);
扫描仪sc=新的扫描仪(System.in);
choice=sc.nextInt();
开关(选择){
案例1:
System.out.println(“准备添加记录”);
//set();
打破
案例2:
System.out.println(“准备删除记录”);
//删除();
打破
案例3:
System.out.println(“准备更新记录”);
//更新();
打破
案例4:
System.out.println(“这是您的记录”);
//显示();
打破
案例5:
System.out.println(“outwego”);
系统出口(0);
//退出();
打破
违约:
System.out.println(“重试”);
打破
}
}而(选择>5 | |选择<1);
}
}
虽然我有用户输入,但我希望显示选项
每次操作(添加、删除..)完成后直到用户按下
退出。
您可以设置int flag=0代码>并且当用户选择退出选项时,将flag设置为1以通知循环退出。现在,您已经在默认情况下为编号>5
或<1
进行了中断,因此无需在时将该条件置于中
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 );
严肃编辑
作为Java程序员,我可能建议您使用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
还有一件事:
用环绕代码,尝试捕捉以省去无效输入,并再次提示输入。
大多数情况下,不建议接受异常
do{
try{
....//Your Switch case
}catch(InputMismatchException e){}
} while (choice !=5);//But remove System.exti(0); from your switch statement
只需将while条件更改为:
} while ( choice > 0 && choice < 5 );
}while(选项>0&&choice<5);
首先确保扫描仪确实有一个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 );
}
}
只需将while循环设置为:
} while ( choice!=0 );(The Wrong one)
更正:
} while(choice!=0 && choice<=5)
(选择!=0和& Couice,但将此条件,无论如何考虑代码< >默认< /代码>情况。在打印“重试”之后,这不会中断循环吗?!!!!@ NICCOW是有意义的,当然,条件可以更改为<代码>(选择!=5)。
-取决于OP想要什么。我认为如果用户输入有效/无效的输入,但OP希望在单次操作后提示输入,则会退出程序。@TAsk在有效输入时不会退出(除非是5,表示退出)。至于无效输入-请参阅我上面的评论。@nIcEcOw,因为他是一名c/c++程序员:p因为我更喜欢整数而不是布尔;)不要介意应该使用布尔值。因为它只提供标记。啊哈,因为我也经常为这件事放整数,尽管我一直希望自己在这种行为中改为布尔值,+!尽管对所有人来说:-)@任务,所以不要表现得像一个!作为一名自豪的Java程序员,使用布尔/布尔作为二进制标志!:实际上,如果是认真的话,我认为根本没有必要使用标志
:要么像用户最初做的那样退出,要么简单的返回
,这两个选项都允许您执行:while(true)
。还有@nIcEcOw-它并不总是关于大小:P-1既然你来晚了,你至少应该仔细阅读这个问题以及已经公布的答案。@alfasin这个-1不打扰我,因为我来晚了,所以我半小时前才醒来,刚刚看到这个问题。So@AmiteshRai:请,一定要重新考虑这个答案。为什么有两个值退出循环(5
(它还强制关闭应用程序)和0
)?虽然我对此投了赞成票,但我仍然感到惊讶,为什么回答的人从来没有在他们的代码中看到OP做错了什么。在每次迭代do while
循环时,需要创建对Scanner
对象的新引用吗?为什么不把那部分放在那东西外面?只是有点好奇:-)@nIcEcOw还有:为什么这个代码永远不会进入if
状态?@alfasin:Ahha,我完全错过了那部分,因为每次运行都没有Scanner
功能:-)我今天不回答,好像我的头今天没有看到这些东西,所以我可以给你一些不好的建议:——)@nIcEcOw我也有这样的日子,愿原力与你同在!