我目前正在制作一个程序,允许您选择4个选项中的1个,为了正确执行此操作,我必须使用循环。这个简单的程序的要点是允许用户选择1 4种选择:命令提示冻结后按回车,无效循环
1 - 由 2设置%至AVG - 输入级(并采取进入那些成绩平均) 3 - 获取平均(我假设平均等级和平均百分比) 4 - 退出
目前,我没有得到编译错误,并且能够运行该程序。每当我输入1并按回车,出于某种原因,号码进入,但它只是一个空格,我不得不按CTRL-C解冻它。我也不知道如何获得“while(choice!= 1);”行来正确执行。
我需要让程序循环,允许用户按他们想要的次数执行所有的选项,直到他们按下4退出,所以我使用了一个定点控制的循环。这是我的代码,我是初学者,所以我可能没有完成整个“循环”过程。谢谢!
import java.util.Scanner;
public class ExerciseThree
{
public static void main (String[] argsv)
{
float percent = 0;
double grade = 0;
double totalAvg = 0;
double total = 0;
double gradeAvg = 0;
int gradeCounter = 0;
int quit;
int choice;
int choiceOne;
Scanner input = new Scanner (System.in);
System.out.println("Please choose one of the following: \n 1 - Set percentage of total for new grades \n 2 - Enter new grades \n 3 - Get average \n 4 - Quit ");
choice = input.nextInt();
while (choice != 4);
switch (choice)
{
case 1:
if(choice == 1) {
System.out.println("Enter a percentage to multiply by");
percent = input.nextFloat();
break;
}
case 2:
if (choice == 2) {
System.out.println("Enter grades");
grade = input.nextDouble();
total = total + grade;
gradeCounter = gradeCounter + 1;
gradeAvg = (double) total/gradeCounter;
break;
}
case 3:
if (choice == 3) {
System.out.println("You have chosen to get the average");
totalAvg = totalAvg + percent * grade;
totalAvg = input.nextDouble();
break;
}
default:
if (choice == 4){
System.out.println("You have chosen to quit");
quit = input.nextInt();
break;
}
}
'如果(选择!= 4 ||选择== 4)' - 这总是'true':/ – Maroun
哦,我明白了!因为任何选择!= 4也会使1,2或3计数为!= 4 – user3304333
此外,当您切换(选择)时,此后不需要“if”。因为......你开关(选择)已经做到了。 – Maroun