转换语句和百分比



如何在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。

对于情形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%)。

最新更新