2015-10-20 50 views
1

如何在開關語句中乘以百分比和用戶輸入數字?我正在製作一個程序,需要用戶輸入一些數字並以各種方式進行修改,但是我被困在百分比部分。開關語句和百分比

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

您可以使用'Scanner.nextDouble'來請求百分比,然後在乘以之前除以100。 –

回答

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%)分攤。