Java 我怎样才能重复这句话;输入标记或等级“;在while循环中使用。在这种情况下,每次控制返回到循环的开始,以再次输入主题?
如何在while循环中重复语句“输入分数或等级”。在这种情况下,每次控制返回循环的开始。我想重复输入标记直到用户想离开Java 我怎样才能重复这句话;输入标记或等级“;在while循环中使用。在这种情况下,每次控制返回到循环的开始,以再次输入主题?,java,loops,error-handling,while-loop,flow-control,Java,Loops,Error Handling,While Loop,Flow Control,如何在while循环中重复语句“输入分数或等级”。在这种情况下,每次控制返回循环的开始。我想重复输入标记直到用户想离开 公共类标记2{ 公共静态void main(字符串参数[]){ 扫描仪s=新的扫描仪(System.in); System.out.println(“这是主题列表”); System.out.println(“1-物理”); System.out.println(“2-Math”); 系统输出打印(“3-计算机”); 布尔输入=真; while(输入){ System.out
公共类标记2{
公共静态void main(字符串参数[]){
扫描仪s=新的扫描仪(System.in);
System.out.println(“这是主题列表”);
System.out.println(“1-物理”);
System.out.println(“2-Math”);
系统输出打印(“3-计算机”);
布尔输入=真;
while(输入){
System.out.println(“在此输入受试者编号”);
String sub=s.nextLine();
如果(子项等于(“1”)){
System.out.println(“物理学”);
System.out.println(“”);
System.out.println(“输入标记/输入更改以进行更改/输入退出以进行休假”);
字符串change1=null;
字符串change2=null;
整数分=0;
试一试{
change1=s.nextLine();
marks=Integer.parseInt(change1);
}捕获(例外e){
System.out.println(“请只输入字符串值”);
change2=s.nextLine();
如果(变更2.等于(“变更”)){
持续
}else if(change2.equals(“退出”)){
系统出口(0);
}
}
如果(marks您需要在第一个while循环中使用另一个while循环,如果我理解正确,您希望在输入change后更改主题?如果是这种情况,那么这将起作用:
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Here is Subject list");
System.out.println("1-Physics");
System.out.println("2-Math");
System.out.println("3-computer");
setSubject:
while (true)
{
System.out.println("Enter subject number here");
String sub = s.nextLine();
if (sub.equals("1"))
{
System.out.println("Physics");
int marks = 0;
while (true)
{
System.out.println("Enter marks /Enter change for change /Enter exit for leave");
if (s.hasNextInt())
{
marks = s.nextInt();
s.nextLine();
} else
{
String command = s.nextLine();
if (command.equalsIgnoreCase("exit")) break setSubject;
else if (command.equalsIgnoreCase("change")) continue setSubject;
else
{
System.out.println("Please enter a valid option");
continue;
}
}
if (marks < 40)
{
System.out.println("Student is fail");
} else if (marks == 40)
{
System.out.println("Student is fail he need more practice");
} else if (marks < 70)
{
System.out.println("need more practice but also good");
} else if (marks == 70)
{
System.out.println("Good");
} else if (marks < 90)
{
System.out.println("Good but also Excellent");
} else if (marks == 90)
{
System.out.println("Excellent");
} else if (marks < 100)
{
System.out.println("Outstanding");
} else if (marks == 100)
{
System.out.println("Good but also excellent");
} else
{
System.out.println("");
}
}
}
}
}
使用一个while循环:
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Here is Subject list");
System.out.println("1-Physics");
System.out.println("2-Math");
System.out.println("3-computer");
String subject = "";
int marks = 0;
while (true)
{
if (subject.isEmpty())
{
System.out.println("Enter subject number here");
subject = s.nextLine();
}
if (subject.equals("1"))
{
System.out.println("Physics");
System.out.println("Enter marks /Enter change for change /Enter exit for leave");
if (s.hasNextInt())
{
marks = s.nextInt();
s.nextLine();
} else
{
String command = s.nextLine();
if (command.equalsIgnoreCase("exit")) break ;
else if (command.equalsIgnoreCase("change")) {
subject = "";
continue ;
}
else
{
System.out.println("Please enter a valid option");
continue;
}
}
if (marks < 40)
{
System.out.println("Student is fail");
} else if (marks == 40)
{
System.out.println("Student is fail he need more practice");
} else if (marks < 70)
{
System.out.println("need more practice but also good");
} else if (marks == 70)
{
System.out.println("Good");
} else if (marks < 90)
{
System.out.println("Good but also Excellent");
} else if (marks == 90)
{
System.out.println("Excellent");
} else if (marks < 100)
{
System.out.println("Outstanding");
} else if (marks == 100)
{
System.out.println("Good but also excellent");
} else
{
System.out.println("");
}
marks = 0;
}
}
}
publicstaticvoidmain(字符串参数[])
{
扫描仪s=新的扫描仪(System.in);
System.out.println(“这是主题列表”);
System.out.println(“1-物理”);
System.out.println(“2-Math”);
系统输出打印(“3-计算机”);
字符串主语=”;
整数分=0;
while(true)
{
if(subject.isEmpty())
{
System.out.println(“在此输入受试者编号”);
subject=s.nextLine();
}
如果(主体等于(“1”))
{
System.out.println(“物理学”);
System.out.println(“输入标记/输入更改以进行更改/输入退出以进行休假”);
如果(s.hasNextInt())
{
marks=s.nextInt();
s、 nextLine();
}否则
{
String命令=s.nextLine();
if(command.equalsIgnoreCase(“退出”))中断;
else if(command.equalsIgnoreCase(“change”)){
主语=”;
持续
}
其他的
{
System.out.println(“请输入有效选项”);
持续
}
}
如果(分数<40)
{
System.out.println(“学生不及格”);
}否则如果(分数=40)
{
学生不及格,需要更多的练习;
}否则,如果(分数<70)
{
System.out.println(“需要更多的实践,但也很好”);
}否则如果(分数=70)
{
系统输出打印号(“良好”);
}否则,如果(标记<90)
{
System.out.println(“很好,但也很优秀”);
}否则如果(分数=90)
{
System.out.println(“优秀”);
}否则,如果(分数<100)
{
系统输出打印项次(“未付”);
}否则如果(分数=100)
{
System.out.println(“很好,但也很优秀”);
}否则
{
System.out.println(“”);
}
分数=0;
}
}
}
不使用内部循环是否可以做到这一点?是的,这是可行的,但您必须在循环外部声明变量并检查循环内部的字段值,我认为这是不必要的,但如果您这样说,例如,您可以创建一个主题变量,在循环内部您可以检查它是否已设置,更改也是如此,u可以设置一个布尔值。编辑了我的解决方案
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Here is Subject list");
System.out.println("1-Physics");
System.out.println("2-Math");
System.out.println("3-computer");
String subject = "";
int marks = 0;
while (true)
{
if (subject.isEmpty())
{
System.out.println("Enter subject number here");
subject = s.nextLine();
}
if (subject.equals("1"))
{
System.out.println("Physics");
System.out.println("Enter marks /Enter change for change /Enter exit for leave");
if (s.hasNextInt())
{
marks = s.nextInt();
s.nextLine();
} else
{
String command = s.nextLine();
if (command.equalsIgnoreCase("exit")) break ;
else if (command.equalsIgnoreCase("change")) {
subject = "";
continue ;
}
else
{
System.out.println("Please enter a valid option");
continue;
}
}
if (marks < 40)
{
System.out.println("Student is fail");
} else if (marks == 40)
{
System.out.println("Student is fail he need more practice");
} else if (marks < 70)
{
System.out.println("need more practice but also good");
} else if (marks == 70)
{
System.out.println("Good");
} else if (marks < 90)
{
System.out.println("Good but also Excellent");
} else if (marks == 90)
{
System.out.println("Excellent");
} else if (marks < 100)
{
System.out.println("Outstanding");
} else if (marks == 100)
{
System.out.println("Good but also excellent");
} else
{
System.out.println("");
}
marks = 0;
}
}
}