0

一日中これに問題があります。私を助けてください。問題を表示できません出力には次のように表示されます

問題番号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;
    }



}
4

2 に答える 2

1

あなたの質問が出力されない理由は簡単です。

public void pickOperation(){
    int operation = 0;

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

 }

ローカル変数 を作成し、operationそれに入力値を代入しています。これは、フィールドoperationが設定されないことを意味するため、メソッドswitch内のステートメントgetProblemに関しては、switch ステートメントと一致しないため、何も出力されません。

これを修正するには、int operation = 0;宣言を削除するだけです。


編集

pickDifficultyあなたの方法で同じ問題に気づきました。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;と、JVMoperationはクラス内で名前が付けられた次の使用可能な変数 (フィールド内) を探すようになります。そのため、最初のアサーション ステートメントを削除すると、値operationが残ります。

于 2013-03-10T10:35:22.003 に答える
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";
    }
}
于 2013-03-10T10:34:30.790 に答える