gpt4 book ai didi

Java显示不显示整个方法

转载 作者:行者123 更新时间:2023-12-01 14:47:05 25 4
gpt4 key购买 nike

一整天都在为此烦恼。请帮我。我无法显示问题输出显示

问题 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;
}



}

最佳答案

您的问题未输出的原因很简单。

public void pickOperation(){
int operation = 0;

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

}

您正在创建一个局部变量操作,并向其分配输入值。这意味着 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();
}

一旦该方法完成,操作内的值就会被销毁。它之所以没有保留在您的字段int operation中,是因为您声明了一个更本地化的operation变量。通过删除此方法开头的 int operation = 0;,您可以强制 JVM 在类中查找下一个名为 operation 的可用变量,该变量位于 field 。这就是为什么当您删除第一个断言语句时,您的 operation 值将保留下来。

关于Java显示不显示整个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15321080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com