英文:
How can i repeat statement "enter marks or grade" with in while loop.In this case every time control back to start of loop to enter subject again?
问题
public class Marks2 {
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");
boolean input=true;
while (input){
System.out.println("Enter subject number here");
String sub=s.nextLine();
if(sub.equals("1")) {
System.out.println("Physics");
System.out.println("");
System.out.println("Enter marks /Enter change for change /Enter exit for leave");
String change1 = null;
String change2 = null;
int marks = 0;
try {
change1 = s.nextLine();
marks = Integer.parseInt(change1);
} catch (Exception e) {
System.out.println("Please enter only string value");
change2 = s.nextLine();
if (change2.equals("change")) {
continue;
} else if (change2.equals("exit")) {
System.exit(0);
}
}
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 if(change1.equals("change")){
continue;
}
else if(change2.equals("exit")){
System.exit(0);
}
else {
System.out.println("");
}
continue;
}
}
}
}
继续控制会回到循环开始,并提示用户重新输入科目。是否有可能允许用户输入分数,直到用户想要离开?
英文:
How can i repeat statement "enter marks or grade" with in while loop.In this case every time control back to start of loop. and i want to repeat enter marks until user want to leave?
public class Marks2 {
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");
boolean input=true;
while (input){
System.out.println("Enter subject number here");
String sub=s.nextLine();
if(sub.equals("1")) {
System.out.println("Physics");
System.out.println("");
System.out.println("Enter marks /Enter change for change /Enter exit for leave");
String change1 = null;
String change2 = null;
int marks = 0;
try {
change1 = s.nextLine();
marks = Integer.parseInt(change1);
} catch (Exception e) {
System.out.println("Please enter only string value");
change2 = s.nextLine();
if (change2.equals("change")) {
continue;
} else if (change2.equals("exit")) {
System.exit(0);
}
}
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 if(change1.equals("change")){
continue;
}
else if(change2.equals("exit")){
System.exit(0);
}
else {
System.out.println("");
}
continue;
}
}
}
}
After continue control went to start of loop and ask to enter subject again. Is it possible to enter grade until user wants to leave?
答案1
得分: 1
你需要在第一个循环内部再加入一个 while 循环,如果我理解正确的话,你想在进入 "change" 后更改主题,如果是这样,那么这段代码将会有效:
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("科目列表如下:");
System.out.println("1-物理");
System.out.println("2-数学");
System.out.println("3-计算机");
setSubject:
while (true)
{
System.out.println("请输入科目编号:");
String sub = s.nextLine();
if (sub.equals("1"))
{
System.out.println("物理");
int marks = 0;
while (true)
{
System.out.println("输入分数 / 输入 change 更改科目 / 输入 exit 退出");
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("请输入有效选项");
continue;
}
}
if (marks < 40)
{
System.out.println("学生不及格");
} else if (marks == 40)
{
System.out.println("学生不及格,需要更多练习");
} else if (marks < 70)
{
System.out.println("需要更多练习,但也不错");
} else if (marks == 70)
{
System.out.println("良好");
} else if (marks < 90)
{
System.out.println("不错,也很优秀");
} else if (marks == 90)
{
System.out.println("优秀");
} else if (marks < 100)
{
System.out.println("杰出");
} else if (marks == 100)
{
System.out.println("不错,也很优秀");
} else
{
System.out.println("");
}
}
}
}
}
一个样本运行
科目列表如下:
1-物理
2-数学
3-计算机
请输入科目编号:
1
物理
输入分数 / 输入 change 更改科目 / 输入 exit 退出
change
请输入科目编号:
1
物理
输入分数 / 输入 change 更改科目 / 输入 exit 退出
50
需要更多练习,但也不错
输入分数 / 输入 change 更改科目 / 输入 exit 退出
60
需要更多练习,但也不错
输入分数 / 输入 change 更改科目 / 输入 exit 退出
exit
进程以退出代码 0 结束
使用一个 while 循环:
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("科目列表如下:");
System.out.println("1-物理");
System.out.println("2-数学");
System.out.println("3-计算机");
String subject = "";
int marks = 0;
while (true)
{
if (subject.isEmpty())
{
System.out.println("请输入科目编号:");
subject = s.nextLine();
}
if (subject.equals("1"))
{
System.out.println("物理");
System.out.println("输入分数 / 输入 change 更改科目 / 输入 exit 退出");
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("请输入有效选项");
continue;
}
}
if (marks < 40)
{
System.out.println("学生不及格");
} else if (marks == 40)
{
System.out.println("学生不及格,需要更多练习");
} else if (marks < 70)
{
System.out.println("需要更多练习,但也不错");
} else if (marks == 70)
{
System.out.println("良好");
} else if (marks < 90)
{
System.out.println("不错,也很优秀");
} else if (marks == 90)
{
System.out.println("优秀");
} else if (marks < 100)
{
System.out.println("杰出");
} else if (marks == 100)
{
System.out.println("不错,也很优秀");
} else
{
System.out.println("");
}
marks = 0;
}
}
}
英文:
You need another while loop inside the first one, if i understand right, you want to change the subject once you enter change? if that is the case then this will work:
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("");
}
}
}
}
}
A Sample Run
Here is Subject list
1-Physics
2-Math
3-computer
Enter subject number here
1
Physics
Enter marks /Enter change for change /Enter exit for leave
change
Enter subject number here
1
Physics
Enter marks /Enter change for change /Enter exit for leave
50
need more practice but also good
Enter marks /Enter change for change /Enter exit for leave
60
need more practice but also good
Enter marks /Enter change for change /Enter exit for leave
exit
Process finished with exit code 0
With one while loop:
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;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论