2013-03-11 81 views
0

我被困在下面的代碼3的解決方案。我需要插入一個簡單的數學問題,並且我不能在閱讀我的書和課堂樣本視頻之後爲生活找出這個問題。我希望程序能夠提出這樣的問題:「回答」64「的問題的答案是」8提高到2的權力「。任何人願意幫助我?如果有人能讓我開始,我可以想出另外兩個問題。非常感謝你!!金Java代碼簡單數學

import java.util.Scanner; //allows for input 

public class ASG03 { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); //allows for input 

     //Step 1 - Declare and initialize variables 
     String candidateName = ""; 
     String responseE = ""; 

     int option = 0; 
     double score = 0; 


     if (score <=85) 
      responseE = "Definite"; 
     else if (score <=70) 
      responseE = "Likely"; 
     else if (score <=60) 
      responseE = "Maybe"; 
     else 
      responseE = "No"; 

     String responseI = ""; 

     if (score <=85) 
      responseI = "Yes"; 
     else if (score <=70) 
      responseI = "Yes"; 
     else if (score <=60) 
      responseI = "Yes"; 
     else 
      responseI = "No"; 

     //Step 2 - Process input 

     System.out.println("Enter candidate name: "); 
     candidateName = input.nextLine(); 
     System.out.println("Enter score 0 -100: "); 
     score = input.nextDouble(); 
     System.out.println(); 



     System.out.println("Enter 1 to set employment category "); 
     System.out.println("Enter 2 to set interview possibility "); 
     System.out.println("Enter 3 to view a sample test question "); 
     System.out.println("Enter option now -> "); 
     option = input.nextInt(); 





     //Step 3 and 4 - Process calculations and output 
     switch(option) 
     { 
     case 1: 
      System.out.println("You are now setting the employment category..."); 
      //can use nested if else 
      System.out.println("Employment category = " + responseE); 

      break; 


     case 2: 
      System.out.println("You are now setting the interview possibilities..."); 
      System.out.println("Interview possibilites = " + responseI); 



      break; 

     case 3: 
      System.out.println("You are now viewing a sample test question..."); 
      //use random and power from Math library 


     default: 


     }//end of switch 

    }//end of main 

}//end of class 

回答

0

我需要一點信息才能給你答案。它看起來像代碼想要一個隨機數字生成器,但在你的問題中,你要求8^2或8 * 8。你想要哪一個?我問,因爲隨機數生成比硬編碼數變量

+0

謝謝大家的更正。新班對我來說,我不趕快趕上。 IrishWhiskey(隨機數)我想?!假設提出問題並告訴用戶答案是正確還是不正確......? – 2013-03-11 20:11:26

2

當你運行你的程序有很大不同,在main你有responseE總是被設置爲「定」。因爲:

看看你的代碼的流程:

double score = 0; 
if (score <=85) 
    responseE = "Definite"; 
else if (score <=70) 
... 
... 

第一if總是不滿意,所以它總是會被執行。

而且,即使你將評估responseE你讀的分數後,你需要重新考慮你如何編寫條件..注意,如果​​然後score <= 70 ....

你應該有這樣的事情這樣的:

之前開關

responseE = getResponse(score); 

這裏是方法getResponse

private static String getResponse(double score) { 
    if (score <=85 && score >70) 
    return "Definite"; 
    else if (score <=70 && score > 60) 
    return "Likely"; 
    else if (score <=60 && score > 40) //For example.. 
    return "Maybe"; 
    return "No"; 
} 

同爲要評估你讀的輸入等領域。

+1

簡而言之,您在閱讀score__之前根據分數確定回覆 – 2013-03-11 18:21:17