所以我正在编写这个代码,这是一个关于加法和减法练习的数学程序。我的代码目前是(未完成但编译):
import java.util.Scanner;
import java.util.Arrays;
public class ElementaryMath{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = 1;
do{
try{
System.out.println("-------Elementary Math Practice-------");
System.out.println(
"Please select a mode: \n" +
"1) Addition \n" +
"2) Subtraction "
);
int selection = in.nextInt();
if(selection == 1){
Addition();
}
else if(selection == 2){
Subtraction();
}
else{
selection = selection/0 ;
}
t=2;
}
catch(Exception e){
System.out.println("Invalid input.");
}
}
while(t==1);
}
public static void Addition(){
Scanner in = new Scanner(System.in);
int j = 1;
do{
try{
System.out.println("-------Addition Problem Sets-------");
System.out.println(
"Please select a difficulty: \n" +
"1) Easy \n" +
"2) Medium \n" +
"3) Hard \n "
);
int selection2 = in.nextInt();
if(selection2 == 1){
EasyAddition();
}
else if(selection2 == 2){
MediumAddition();
}
else if(selection2 == 3){
HardAddition();
}
else{
selection2 = selection2/0 ;
}
j=2;
}
catch(Exception e){
System.out.println("Invalid input.");
}
}
while(j==1);
}
public static void Subtraction(){
Scanner in = new Scanner(System.in);
int k = 1;
do{
try{
System.out.println("-------Subtraction Problem Sets-------");
System.out.println(
"Please select a difficulty: \n" +
"1) Easy \n" +
"2) Medium\n" +
"3) Hard \n"
);
int selection3 = in.nextInt();
if(selection3 == 1){
EasySubtraction();
}
else if(selection3 == 2){
MediumSubtraction();
}
else if(selection3 == 3){
HardSubtraction();
}
else{
selection3 = selection3/0 ;
}
k=2;
}
catch(Exception e){
System.out.println("Invalid input.");
}
}
while(k==1);
}
public static void EasyAddition(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the amount of problems you wish to do: ");
int amountEA = in.nextInt();
int correct = 0;
EasyAdder(amountEA, correct);
System.out.println(correct);
int incorrect = amountEA - correct;
System.out.println("You have gotten: " +correct+ " correct!");
System.out.println("You have gotten: " +incorrect+ " incorrect.");
}
public static void MediumAddition(){System.out.println("Test2");}
public static void HardAddition(){System.out.println("Test3");}
public static void EasySubtraction(){System.out.println("Test4");}
public static void MediumSubtraction(){System.out.println("Test5");}
public static void HardSubtraction(){System.out.println("Test6");}
public static int EasyAdder(int amountEA, int correct){
Scanner in = new Scanner(System.in);
int[]answersEasyAdder = new int[amountEA];
int[]usersanswersEasyAdder = new int[amountEA];
for(int z=0; z<amountEA ; z++){
int a = (int)(Math.random()*9+1);
int b = (int)(Math.random()*9+1);
int c = a+b;
answersEasyAdder[z] = c;
System.out.println(Arrays.toString(answersEasyAdder));
System.out.print("What is: " +a+ " + " +b+ " ?");
int d = in.nextInt();
usersanswersEasyAdder[z]=d;
System.out.println(Arrays.toString(usersanswersEasyAdder));
}
for(int l=0; l<amountEA; l++){
if(answersEasyAdder[l]==usersanswersEasyAdder[l]){
correct++;
System.out.println(correct);
}
}
System.out.println(correct);
return correct;
}
}
所以我的问题在于EasyAddition和EasyAdder方法。我将正确的密码传递给EasyAdder,以更新用户正确的问题。我试图返回它,但是(这也是为什么System.out.print无处不在,我试图跟踪变量的进度)变量正确似乎在我返回值后没有得到更新。我的回归没有采用正确的方法吗?我有点失落。
答案 0 :(得分:0)
您没有将返回值分配给预期变量,这是校正
public static void EasyAddition(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the amount of problems you wish to do: ");
int amountEA = in.nextInt();
int correct = EasyAdder(amountEA, correct);
System.out.println(correct);
int incorrect = amountEA - correct;
System.out.println("You have gotten: " +correct+ " correct!");
System.out.println("You have gotten: " +incorrect+ " incorrect.");
}
答案 1 :(得分:0)
方法 easyAdder 正在返回一个迷路的值
这样做:
correct = EasyAdder(amountEA, correct);
这种情况正在发生,因为java正在改变方法内部的int更正值,但这些更改不会影响外部的原语