Java 如何将值从一个方法传递到主方法?
我正在创建一个数学程序,询问用户他们想要使用多少位数,然后询问他们想要做什么样的数学。然后,它根据用户的答案向用户提出10个数学问题。然后它决定它们是对还是错,并显示适当的消息。然后计算他们正确答案的百分比,并根据百分比显示消息 问题在于,该程序在10个问题后从未停止,也从未计算百分比或显示最终消息。我相信这至少是部分发生的,因为我没有从“askQuestion”方法传递新的“answeredTyped”值。有人能解释一下我如何轻松地将这个新值传递回main方法,使它在10停止吗?或者告诉我还有什么其他原因使它无法正常工作 我已经问了很多关于这个程序的问题,所以我很感谢大家对我学习Java和对Java知之甚少的耐心 谢谢你的帮助!(我也为这里的格式错误道歉!在更改了大约一百万个内容之后,它变得很难看)Java 如何将值从一个方法传递到主方法?,java,Java,我正在创建一个数学程序,询问用户他们想要使用多少位数,然后询问他们想要做什么样的数学。然后,它根据用户的答案向用户提出10个数学问题。然后它决定它们是对还是错,并显示适当的消息。然后计算他们正确答案的百分比,并根据百分比显示消息 问题在于,该程序在10个问题后从未停止,也从未计算百分比或显示最终消息。我相信这至少是部分发生的,因为我没有从“askQuestion”方法传递新的“answeredTyped”值。有人能解释一下我如何轻松地将这个新值传递回main方法,使它在10停止吗?或者告诉我还有
import java.util.Scanner;
导入javax.swing.JOptionPane;
公共课堂作业2{
公共静态int-answeredTyped=0;
公共静态int=0;
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
智力难度=1;
字符串[]运算符={“加”、“减”、“倍”、“除以”};
int-selectedOperator=1;
int困难输入=整数
.parseInt(JOptionPane)
.showInputDialog(“请选择难度。输入每个问题要使用的位数。”);
如果(困难输入>0){
困难=困难输入;
}
int算术方法=整数
.parseInt(JOptionPane)
.showInputDialog(“选择要学习的算术题:1=只做加法,2=只做减法,3=只做乘法,4=只做除法,5=随机题”);
选择运算符=算术方法;
new Assignment2().askQuestion(难度,空,selectedOperator,
回答类型、操作员、正确答案);
while(回答类型<10){
询问问题(难度,空,选择运算符,回答键入,
操作员(正确答案);
回答输入++;
如果(回答类型>=10){
如果(((浮动)正确答案/回答类型)>=0.75){
JOptionPane
.showMessageDialog(空,
“祝贺你,你已经准备好进入下一个阶段了!”;
}否则{
JOptionPane.showMessageDialog(null,
“请向你的老师寻求额外的帮助。”);
}
}
}
}
公共静态布尔checkResponse(双primaryInt,双secondaryInt,
字符串运算符(文本,双响应){
if(运算符文本等于(“加号”){
返回(primaryInt+secondaryInt)=响应;
}else if(运算符text.equals(“减号”)){
返回(primaryInt-secondaryInt)=响应;
}else if(operatorText.equals(“times”)){
返回(primaryInt*secondaryInt)=响应;
}else if(运算符text.equals(“除以”)){
返回(primaryInt/secondaryInt)=响应;
}
返回false;
}
公共静态字符串displayResponse(布尔值isCorrect){
intrandomIndex=(int)(Math.floor(Math.random()*(4-1+1))+1);
开关(随机索引){
案例1:
返回正确吗?“非常好!”:“不,请再试一次。”;
案例2:
return是否正确?“太好了!”:“错了,再试一次。”;
案例3:
回答正确吗?“干得好!”:“不要放弃!”;
案例4:
回答正确吗?“继续努力!”:“不,继续努力。”;
}
返回“Oops…”;
}
公共静态void askQuestion(int难度,字符串运算符text,
int selectedOperator,int answeredTyped,String[]运算符,
int(正确答案){
布尔正确答案=假;
双初级=数学能力(数学能力(10,难度-1)
+Math.random()*9*Math.pow(10,难度-1));
double secondaryInt=Math.floor(Math.pow)(10,难度-1)
+Math.random()*9*Math.pow(10,难度-1));
operatorText=(selectedOperator==5)?运算符[(int)数学
.floor(Math.random()*操作符.length)]
:operators[选择Operator-1];
double response=double.parseDouble(JOptionPane
.showInputDialog(“多少是”+primaryInt+”)
+运算符text+“”+secondaryInt+“?”);
correctAnswer=checkResponse(primaryInt,secondaryInt,
operatorText,响应);
showMessageDialog(null,displayResponse(correctResponse));
如果(正确答案)
正确答案++;
}
}
您有两个while循环,因此它将调用askQuestion()十次,然后还有askQuestion自己的while循环:
main()
while(回答键入<10){
提问(问题)
while(!correctAnswer&answeredTyped<10){
尝试只使用一个while循环,并将答案的评估放在askQuestion()之外方法您必须在主方法的while循环中递增answeredType,而不是在另一个方法中,因为您没有将变量声明为全局变量与您的问题无关,您是否使用IDE?大多数IDE都内置了格式化程序。如果您使用Eclipse,请按
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Assignment2 {
public static int answeredTyped = 0;
public static int correctAnswers = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int difficulty = 1;
String[] operators = { "plus", "minus", "times", "divided by" };
int selectedOperator = 1;
int difficultyInput = Integer
.parseInt(JOptionPane
.showInputDialog("Please choose the difficulty. Enter the number of digits to use in each problem."));
if (difficultyInput > 0) {
difficulty = difficultyInput;
}
int arithmeticMethod = Integer
.parseInt(JOptionPane
.showInputDialog("Choose an arithmetic problem to study: 1 = Addition Only, 2 = Subtraction Only, 3 = Multiplication Only, 4 = Division Only, 5 = Random Problems"));
selectedOperator = arithmeticMethod;
new Assignment2().askQuestion(difficulty, null, selectedOperator,
answeredTyped, operators, correctAnswers);
while (answeredTyped < 10) {
askQuestion(difficulty, null, selectedOperator, answeredTyped,
operators, correctAnswers);
answeredTyped++;
if (answeredTyped>= 10) {
if (((float) correctAnswers / answeredTyped) >= 0.75) {
JOptionPane
.showMessageDialog(null,
"Congratulations, you are ready to go on to the next level!");
} else {
JOptionPane.showMessageDialog(null,
"Please ask your teacher for extra help.");
}
}
}
}
public static boolean checkResponse(double primaryInt, double secondaryInt,
String operatorText, double response) {
if (operatorText.equals("plus")) {
return (primaryInt + secondaryInt) == response;
} else if (operatorText.equals("minus")) {
return (primaryInt - secondaryInt) == response;
} else if (operatorText.equals("times")) {
return (primaryInt * secondaryInt) == response;
} else if (operatorText.equals("divided by")) {
return (primaryInt / secondaryInt) == response;
}
return false;
}
public static String displayResponse(boolean isCorrect) {
int randomIndex = (int) (Math.floor(Math.random() * (4 - 1 + 1)) + 1);
switch (randomIndex) {
case 1:
return isCorrect ? "Very Good!" : "No. Please try again.";
case 2:
return isCorrect ? "Excellent!" : "Wrong. Try once more.";
case 3:
return isCorrect ? "Nice Work!" : "Don\'t give up!";
case 4:
return isCorrect ? "Keep up the good work!" : "No. Keep trying.";
}
return "Oops...";
}
public static void askQuestion(int difficulty, String operatorText,
int selectedOperator, int answeredTyped, String[] operators,
int correctAnswers) {
boolean correctAnswer = false;
double primaryInt = Math.floor(Math.pow(10, difficulty - 1)
+ Math.random() * 9 * Math.pow(10, difficulty - 1));
double secondaryInt = Math.floor(Math.pow(10, difficulty - 1)
+ Math.random() * 9 * Math.pow(10, difficulty - 1));
operatorText = (selectedOperator == 5) ? operators[(int) Math
.floor(Math.random() * operators.length)]
: operators[selectedOperator - 1];
double response = Double.parseDouble(JOptionPane
.showInputDialog("How much is " + primaryInt + " "
+ operatorText + " " + secondaryInt + "?"));
correctAnswer = checkResponse(primaryInt, secondaryInt,
operatorText, response);
JOptionPane.showMessageDialog(null, displayResponse(correctAnswer));
if (correctAnswer)
correctAnswers++;
}
}
while (answeredTyped < 10) {
while (!correctAnswer && answeredTyped < 10) {