如何在switch语句中乘以百分比和用户输入数字?我正在制作一个程序,它接受一些用户输入并以各种方式改变它,但我仍然坚持百分比部分。
int userChoice; //The user's choice from the menu.
double percentCurve = .1; //A 10% increase to the user's grade the user chooses by selecting option 2.
int userExtraPercent; //The percentage the user chooses to add to their grade by choosing option 4.
userChoice = keyboard.nextInt ();
switch( userChoice) {
case 2:
System.out.println("Curve applied: " + percentCurve);
double adjustedGradeTwo = userGrade * percentCurve;
System.out.println("Adjusted grade: " + adjustedGradeTwo);
break;
//Takes user's grade and adds 10 percent.
case 4:
System.out.println("Enter the percentage of the curve: ");
userExtraPercent = keyboard.nextInt ();
System.out.println("Curve applied: " + userExtraPercent + "%");
int adjustedGradeFour = userGrade * userExtraPercent;
System.out.println("Adjusted grade: " + adjustedGradeFour);
break;
//Adds a percentage chosen by the user to their initial grade.
}
我的switch语句中还有其他三种可能的选择,但我编辑了这些,因为它们正在工作,甚至我感到困惑,我写了一些血腥的东西。 XD
无论如何,我需要弄清楚的是如何获取用户的号码,将其转换为百分比,乘以用户的等级并将结果显示给用户。 IE,用户的等级为85,他们想要额外的12%,我的程序给他们95.2的等级。
答案 0 :(得分:0)
这样的事情适用于案例4:
System.out.println("Enter the percentage of the curve: ");
double userExtraPercent = keyboard.nextDouble();
double adjustedGradeFour = userGrade + (userGrade * (userExtraPercent / 100.0));
System.out.println("Curve applied: " + userExtraPercent + "%");
System.out.println("Adjusted grade: " + adjustedGradeFour);
使用Scanner.nextDouble
,然后将其除以100(假设他们以12%的比例输入12)。