2013-03-10 94 views
0

這一整天都有麻煩。請幫幫我。我不能讓這個問題顯示 輸出顯示Java顯示不顯示整個方法

問題號1

回答:0

正確....

問題號2

答案:1

錯....

它必須證明:

問題號1

10 + 11 =?

答:21

正確... */

import java.util.Random; 
import java.util.*; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 
import java.lang.Math; 


public class MathIsSuperFun1{ 

Scanner input = new Scanner(System.in); 
int correctAnswers; 
int randomNum1; 
int randomNum2; 
int choice; 
int corrrectAnswers, wrongAnswers; 
String playerName =""; 
int operation; 
int userAnswer; 
int correctAnswer = 0; 
int userRemainder, correctRemainder; 
int x = 0; 
int temporaryNum1, temporaryNum2; 
int range; 
int randomNumber; 


public static void main (String args[]){ 
MathIsSuperFun1 lab = new MathIsSuperFun1(); 
     lab.init(); 
    } 

public void init(){ 

getName(); 
pickDifficulty(); 
pickOperation(); 
for(int x = 0; x < 10; x++) 
{ 
System.out.println("\t\t\t~~~~~~~PROBLEM NUMBER" + (x + 1) + "~~~~~~~~"); 
assignNum(); 
getProblem(); 
checkAnswer(); 

} 
} 

//GET PLAYER NAME USING PANE 
public static String getName(){ 
String playerName; 


playerName = JOptionPane.showInputDialog(null, "Welcome!\nEnter your name and press OK.", "Math Is Super Fun!", JOptionPane.QUESTION_MESSAGE); 
System.out.println("Do your best," + playerName + "!"); 
return playerName; 
} 

//GET PROBLEM BASED ON OPERATION 
    public void getProblem(){ 
    switch(operation){ 

     case 1: 
      System.out.println(randomNum1 + "+" + randomNum2 + "= ?\n"); 
      correctAnswer = randomNum1 + randomNum2; 
      break; 

     case 2: 
      System.out.println(randomNum1 + "-" + randomNum2 + "= ?\n"); 
      correctAnswer = randomNum1-randomNum2; 
      break; 
     case 3: 
      System.out.println(randomNum1 + "*" + randomNum2 + "= ?\n"); 
      correctAnswer = randomNum1*randomNum2; 
      break; 
     case 4: 
      System.out.println(randomNum1 + "/" + randomNum2 + "= ?\n"); 
      correctAnswer = randomNum1/randomNum2; 
      correctRemainder = randomNum1%randomNum2; 
      break; 
    } 

     System.out.print("Answer: "); 
     userAnswer = input.nextInt(); 
     if(operation == 4){ 
     System.out.print("Remainder: "); 
     userRemainder = input.nextInt(); 
     } 
     return 0; 



    } 



//PICK DIFFICULTY USING DIALOG 
public void pickDifficulty(){ 
int choice = 0; 
System.out.println("1 - Child's Play\n2 - No Sweat\n3 - Bitter\n4 - Cold-blooded\n5 - Brutal\n6 - Genius"); 
choice = input.nextInt(); 
} 

//PICK OPERATIONS 
public void pickOperation(){ 
int operation = 0; 

System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division "); 
operation = input.nextInt(); 

} 

//GET NUMBER RANGE BASED ON DIFFICULTY 
public int numberRange(){ 
int range = 0; 

switch(choice){ 

case 1: 
range = 100; 
break; 

case 2: 
range = 1000; 
break; 

case 3: 
range = 10000; 
break; 

case 4: 
range = 100000; 
break; 

case 5: 
range = 1000000; 
break; 

case 6: 
range = 10000000; 
break; 
} 
return range; 
} 

//GET CORRECT RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD 
public void correctResponse(){ 
String responseCorrect = ""; 

switch (getRandom(5)){ 

     case 1: 
     responseCorrect = "Correct. Keep up the good work!"; 
     break; 

     case 2: 
     responseCorrect = "Correct. Keep aiming higher!"; 
     break; 

     case 3: 
     responseCorrect = "Correct. Well done!"; 
     break; 

     case 4: 
     responseCorrect = "Correct. Nice work!"; 
     break; 

     case 5: 
     responseCorrect = "Correct. We're almost there!"; 
     break; 
     } 

     System.out.println(responseCorrect); 
     correctAnswers += 1; 
} 

//GET WRONG RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD 
public String wrongResponse(){ 
String responseWrong = ""; 
switch (getRandom(5)){ 

     case 1: 
     responseWrong = "Wrong. Don't give up!"; 
     break; 

     case 2: 
     responseWrong = "Wrong. You can do it!"; 
     break; 

     case 3: 
     responseWrong = "Wrong. Try again puny human!"; 
     break; 

     case 4: 
     responseWrong = "Wrong. You must be really weak at math!"; 
     break; 

     case 5: 
     responseWrong = "Wrong. I pity you!"; 
     break; 
     } 

     System.out.println(responseWrong); 
     System.out.println("The correct answer is:" + correctAnswer); 
     if(operation == 4) 
     System.out.println("Correct Remainder: " + correctRemainder); 
     return responseWrong; 
    } 




public void checkAnswer(){ 

     if(operation != 4 && userAnswer == correctAnswer){ 
      correctResponse(); 
     } 
     else if(operation == 4 && userAnswer == correctAnswer && userRemainder == correctRemainder){ 
      correctResponse(); 
     } 
     else{ 
      wrongResponse(); 
     } 
    } 

public void assignNum(){ 

    int temporaryNum1 = getRandom(numberRange()); 
    int temporaryNum2 = getRandom(numberRange()); 
    while(operation == 4 && temporaryNum1 == 0){ 
      temporaryNum1 = getRandom(numberRange()); 
     } 
     while(operation == 4 && temporaryNum2 == 0){ 
      temporaryNum2 = getRandom(numberRange()); 
     } 
     if(temporaryNum1 > temporaryNum2) 
     { 
      randomNum1 = temporaryNum1; 
      randomNum2 = temporaryNum2; 
     } 
     else 
     { 
      randomNum1 = temporaryNum2; 
      randomNum2 = temporaryNum1; 
     } 
    } 



    public int getRandom(int range){ 
    randomNumber = (int)Math.floor((Math.random()*range)+1); 
    return randomNumber; 
    } 



} 
+0

你的代碼甚至沒有編譯。首先,'getProblem()'不應該返回任何東西,而是返回'0'。 – SudoRahul 2013-03-10 10:17:38

+0

和'getAnswers()'方法在哪裏? – 2013-03-10 10:18:42

+0

嗯,我忘了刪除它,它已經包括在方法getProblem(),在開關case語句之後... – 2013-03-10 10:21:41

回答

0

問題是:int operation = 0;你需要採取行動的價值操作,你在創建它主要變量

試試這個:

import java.util.Random; 
import java.util.*; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 
import java.lang.Math; 

public class Main { 

    Scanner input = new Scanner(System.in); 
    int correctAnswers; 
    int randomNum1; 
    int randomNum2; 
    int choice; 
    int corrrectAnswers, wrongAnswers; 
    String playerName = ""; 
    int operation; 
    int userAnswer; 
    int correctAnswer = 0; 
    int userRemainder, correctRemainder; 
    int x = 0; 
    int temporaryNum1, temporaryNum2; 
    int range; 
    int randomNumber; 

    public static void main(String args[]) { 
     Main lab = new Main(); 
     lab.init(); 
    } 

    public void init() { 

     getName(); 
     pickDifficulty(); 
     pickOperation(); 
     for (int x = 0; x < 10; x++) { 
      System.out.println("\t\t\t~~~~~~~PROBLEM NUMBER" + (x + 1) + "~~~~~~~~"); 
      assignNum(); 
      getProblem(); 
      getAnswers(); 
      checkAnswer(); 

     } 
    } 

//GET PLAYER NAME USING PANE 
    public static String getName() { 
     String playerName; 


     playerName = JOptionPane.showInputDialog(null, "Welcome!\nEnter your name and press OK.", "Math Is Super Fun!", JOptionPane.QUESTION_MESSAGE); 
     System.out.println("Do your best," + playerName + "!"); 
     return playerName; 
    } 

//GET PROBLEM BASED ON OPERATION 
    public void getProblem() { 
     switch (operation) { 
      case 1:    
       System.out.println(randomNum1 + "+" + randomNum2 + "= ?\n"); 
       correctAnswer = randomNum1 + randomNum2; 
       break; 

      case 2: 
       System.out.println(randomNum1 + "-" + randomNum2 + "= ?\n"); 
       correctAnswer = randomNum1 - randomNum2; 
       break; 
      case 3: 
       System.out.println(randomNum1 + "*" + randomNum2 + "= ?\n"); 
       correctAnswer = randomNum1 * randomNum2; 
       break; 
      case 4: 
       System.out.println(randomNum1 + "/" + randomNum2 + "= ?\n"); 
       correctAnswer = randomNum1/randomNum2; 
       correctRemainder = randomNum1 % randomNum2; 
       break; 

     } 

     System.out.print("Answer: "); 
     userAnswer = input.nextInt(); 
     if (operation == 4) { 
      System.out.print("Remainder: "); 
      userRemainder = input.nextInt(); 
     } 
     // return 0; 



    } 

//PICK DIFFICULTY USING DIALOG 
    public void pickDifficulty() { 
     System.out.println("1 - Child's Play\n2 - No Sweat\n3 - Bitter\n4 - Cold-blooded\n5 - Brutal\n6 - Genius"); 
     choice = input.nextInt(); 
    } 

//PICK OPERATIONS 
    public void pickOperation() { 
     System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division "); 
     operation = input.nextInt(); 

    } 

//GET NUMBER RANGE BASED ON DIFFICULTY 
    public int numberRange() { 
     int range = 0; 

     switch (choice) { 

      case 1: 
       range = 100; 
       break; 

      case 2: 
       range = 1000; 
       break; 

      case 3: 
       range = 10000; 
       break; 

      case 4: 
       range = 100000; 
       break; 

      case 5: 
       range = 1000000; 
       break; 

      case 6: 
       range = 10000000; 
       break; 
     } 
     return range; 
    } 

//GET CORRECT RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD 
    public void correctResponse() { 
     String responseCorrect = ""; 

     switch (getRandom(5)) { 

      case 1: 
       responseCorrect = "Correct. Keep up the good work!"; 
       break; 

      case 2: 
       responseCorrect = "Correct. Keep aiming higher!"; 
       break; 

      case 3: 
       responseCorrect = "Correct. Well done!"; 
       break; 

      case 4: 
       responseCorrect = "Correct. Nice work!"; 
       break; 

      case 5: 
       responseCorrect = "Correct. We're almost there!"; 
       break; 
     } 

     System.out.println(responseCorrect); 
     correctAnswers += 1; 
    } 

//GET WRONG RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD 
    public String wrongResponse() { 
     String responseWrong = ""; 
     switch (getRandom(5)) { 

      case 1: 
       responseWrong = "Wrong. Don't give up!"; 
       break; 

      case 2: 
       responseWrong = "Wrong. You can do it!"; 
       break; 

      case 3: 
       responseWrong = "Wrong. Try again puny human!"; 
       break; 

      case 4: 
       responseWrong = "Wrong. You must be really weak at math!"; 
       break; 

      case 5: 
       responseWrong = "Wrong. I pity you!"; 
       break; 
     } 

     System.out.println(responseWrong); 
     System.out.println("The correct answer is:" + correctAnswer); 
     if (operation == 4) { 
      System.out.println("Correct Remainder: " + correctRemainder); 
     } 
     return responseWrong; 
    } 

    public void checkAnswer() { 

     if (operation != 4 && userAnswer == correctAnswer) { 
      correctResponse(); 
     } else if (operation == 4 && userAnswer == correctAnswer && userRemainder == correctRemainder) { 
      correctResponse(); 
     } else { 
      wrongResponse(); 
     } 
    } 

    public void assignNum() { 

     int temporaryNum1 = getRandom(numberRange()); 
     int temporaryNum2 = getRandom(numberRange()); 
     while (operation == 4 && temporaryNum1 == 0) { 
      temporaryNum1 = getRandom(numberRange()); 
     } 
     while (operation == 4 && temporaryNum2 == 0) { 
      temporaryNum2 = getRandom(numberRange()); 
     } 
     if (temporaryNum1 > temporaryNum2) { 
      randomNum1 = temporaryNum1; 
      randomNum2 = temporaryNum2; 
     } else { 
      randomNum1 = temporaryNum2; 
      randomNum2 = temporaryNum1; 
     } 
    } 

    public int getRandom(int range) { 
     randomNumber = (int) Math.floor((Math.random() * range) + 1); 
     return randomNumber; 
    } 

    private void getAnswers() { 
     ////////////////Not yet implemented"; 
    } 
} 
+0

非常感謝〜〜 – 2013-03-10 10:55:17

+0

歡迎您:) – 2013-03-10 10:57:31

1

您的問題不輸出的原因很簡單。

public void pickOperation(){ 
    int operation = 0; 

    System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division "); 
    operation = input.nextInt(); 

} 

您正在創建局部變量operation,併爲其分配輸入值。這意味着從未設置字段operation,因此,如果涉及getProblem方法中的switch語句,它將不會輸出任何內容,因爲它與switch語句不匹配。

要解決此問題,只需刪除int operation = 0;聲明。


編輯

pickDifficulty方法只注意到了同樣的問題。我強烈建議你看看this tutorial on scope in Java

你的問題的說明

好。因此,讓我們看看你的代碼:

public void pickOperation(){ 

    int operation = 0; 
    // Declare an int, called 'operation'. 
    System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division "); 
    // Set the newly declared value to an int from the keyboard. 
    operation = input.nextInt(); 
} 

只要這種方法完成,內部operation值被破壞。之所以沒有留在你的領域int operation,是因爲你宣佈了更多的本地operation變量。通過在此方法開始時刪除int operation = 0;,您可以強制JVM查找名爲operation的下一個可用變量,該變量位於該字段中。這就是爲什麼當你刪除第一個斷言陳述時,你的價值operation將繼續存在。

+1

同樣的問題存在挑難度 – Breavyn 2013-03-10 10:37:46

+0

Aye ,只是編輯它。 – christopher 2013-03-10 10:37:58

+0

不是它導致問題,但相同的範圍問題在assignNum()以及 – Breavyn 2013-03-10 10:38:51