Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换语句和百分比_Java - Fatal编程技术网

Java 转换语句和百分比

Java 转换语句和百分比,java,Java,如何在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;

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


无论如何,我需要弄清楚的是如何获取用户的数字,将其转换为百分比,乘以用户的分数,然后将结果显示给用户。也就是说,用户的分数是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%)。

类似的内容适用于案例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%)

您可以使用
Scanner.nextDouble
要求百分比,然后在乘法之前将其除以100。您可以使用
Scanner.nextDouble
要求百分比,然后在乘法之前把它除以100。