好的,这是我在这里的第一篇文章。(善待我 :)) 我就像 Java 两周大,想创建一个应用程序,可以完成总分、平均分和平均分的工作。我的问题是。一个操作结束后如何返回到特定的代码行 例如:我希望代码运行String menu1="Input the number of the operation you want"; 在找到总数或平均值之后。提前致谢
import java.util.Scanner;
public class grader
{
public static void main (String args[])
{
Scanner input=new Scanner(System.in);
System.out.println ("Input your Chemistry marks");
float a= input.nextFloat();
System.out.println ("Input your Biology marks");
float b= input.nextFloat();
System.out.println ("Input your Physics marks");
float c= input.nextFloat();
String menu1="Input the number of the operation you desire ";
String op1="1. Total ";
String op2="2. Average ";
String op3="3. Grade ";
String op4="4. Exit ";
System.out.println (menu1+op1+op2+op3+op4);
Scanner menuselect=new Scanner(System.in);
int option= input.nextInt();
float tot=(a+b+c);
float avg=((a+b+c)/3);
if (option==1)
System.out.println ("Your total is "+tot);
else if (option==2)
System.out.println ("Your average is "+avg);
else if (option==3)
if (avg<0.0)
System.out.println ("Please input proper data");
else if (avg<=49.9)
System.out.println ("Fail");
else if (avg<=69.9)
System.out.println ("Pass");
else if (avg<=100.0)
System.out.println ("Excellent");
else
System.out.println ("Please input proper data");
}
}
Run Code Online (Sandbox Code Playgroud)
你应该使用do-while循环。它的工作原理是执行一些代码,检查条件是否计算为true,如果是,则再次执行代码。
do {
//your code here
}
while (/*some condition*/);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15262 次 |
| 最近记录: |