如果您需要任何解释,请随时提出。 EquationsAndAnswers类的目标是生成8个数组,4个字符串类型和4个double类型,并使用加法公式填充它们,然后为每个基本数学运算符求答等等。然后,从这8个阵列中,应该从四个弦类型方程阵列中的任何一个中随机选择一个方程式。选择随机方程式和要写入的相应答案后,将它们写入文件(equations.txt)。 我的问题是,在从Menu类调用writeBoth方法之后,equations.txt文件看起来像这样:
6 + 10
6 + 10
8 - 9
8 - 9
6 + 10
8 - 9
6 + 10
而不是像这样:
1+2
3-2
4*5
10÷2
我的课程菜单:
public class Menu {
public static boolean additionBool = true;
public static boolean subtractionBool = true;
public static boolean multiplicationBool = true;
public static boolean divisionBool = true;
public static boolean doubleDigitBool = false;
new EquationsAndAnswers().writeBoth();
}
我的类EquationsAndAnswers:
import java.util.Random;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
public class EquationsAndAnswers {
/*Random Generators*/
Random singleDigitNaturalGen = new Random();
Random randomEquationPicker = new Random();
Random doubleDigitNaturalGen = new Random();
public void writeBoth(){
/*Declaration of Four Basic Mathematic Operation Equation Arrays and Corresponding Answers*/
String additionArrayEquation[] = new String[100];
int additionArrayAnswers[] = new int[100];
String subtractionArrayEquation[] = new String[100];
int subtractionArrayAnswers[] = new int[100];
String multiplicationArrayEquation[] = new String[100];
int multiplicationArrayAnswers[] = new int[100];
String divisionArrayEquation[] = new String[100];
double divisionArrayAnswers[] = new double[100];
String assortedEquations[] = new String[100];
double assortedAnswers[] = new double[100];
/*Population of Basic Operation Equation Arrays*/
/*Population of Addition Equation and Answer Arrays*/
for(int additionArrayGenerationX = 0; additionArrayGenerationX < additionArrayEquation.length; additionArrayGenerationX++){
int tempAdditionY = singleDigitNaturalGen.nextInt(11), tempAdditionX = singleDigitNaturalGen.nextInt(11);
int answer = tempAdditionY + tempAdditionX;
String equation = (tempAdditionY + " + " + tempAdditionX);
additionArrayEquation[additionArrayGenerationX] = equation;
additionArrayAnswers[additionArrayGenerationX] = answer;
}
/*Population of Subtraction Equation and Answer Arrays*/
for(int subtractionArrayGenerationX = 0; subtractionArrayGenerationX < subtractionArrayEquation.length; subtractionArrayGenerationX++){
int tempSubtractionY = singleDigitNaturalGen.nextInt(11), tempSubtractionX = singleDigitNaturalGen.nextInt(11);
int answer = tempSubtractionY - tempSubtractionX;
String equation = (tempSubtractionY + " - " + tempSubtractionX);
subtractionArrayEquation[subtractionArrayGenerationX] = equation;
subtractionArrayAnswers[subtractionArrayGenerationX] = answer;
}
/*Population of Multiplication Equation and Answer Arrays*/
for(int multiplicationArrayGenerationX = 0; multiplicationArrayGenerationX < multiplicationArrayEquation.length; multiplicationArrayGenerationX++){
int tempMultiplicationY = singleDigitNaturalGen.nextInt(11), tempMultiplicationX = singleDigitNaturalGen.nextInt(11);
int answer = tempMultiplicationY*tempMultiplicationX;
String equation = "";
multiplicationArrayEquation[multiplicationArrayGenerationX] = equation;
multiplicationArrayAnswers[multiplicationArrayGenerationX] = answer;
}
/*Population of Division Equation and Answer Arrays*/
for(int divisionArrayGenerationX = 0; divisionArrayGenerationX < divisionArrayEquation.length; divisionArrayGenerationX++){
int tempDivisionY = singleDigitNaturalGen.nextInt(11), tempDivisionX = singleDigitNaturalGen.nextInt(11);
double answer = ((double)tempDivisionY / (double)tempDivisionX);
String equation = "";
divisionArrayEquation[divisionArrayGenerationX] = equation;
divisionArrayAnswers[divisionArrayGenerationX] = answer;
}
/*Deciding on Equations to be displayed.*/
int i = 0;
System.out.println(additionArrayEquation[0]);//dev
/*This puts the equations in a random order in the assortedEquations array, in preparation to be written to the Equations.txt file*/
for(int epsilon = 0; epsilon < assortedEquations.length; epsilon++){
int equationType = randomEquationPicker.nextInt(4);
if(equationType == 0){
if(Menu.additionBool = true){
assortedEquations[epsilon] = additionArrayEquation[i];
assortedAnswers[epsilon] = additionArrayAnswers[i];
}
else{
assortedEquations[epsilon] = "Bonus: Enter 0";
}
}
else if(equationType == 1){
if(Menu.subtractionBool = true){
assortedEquations[epsilon] = subtractionArrayEquation[i];
assortedAnswers[epsilon] = subtractionArrayAnswers[i];
}
else{
assortedEquations[epsilon] = "Bonus: Enter 0";
}
}
else if(equationType == 2){
if(Menu.multiplicationBool = true){
assortedEquations[epsilon] = multiplicationArrayEquation[i];
assortedAnswers[epsilon] = multiplicationArrayAnswers[i];
}
else{
assortedEquations[epsilon] = "Bonus: Enter 0";
}
}
else{
if(Menu.divisionBool = true){
assortedEquations[epsilon] = divisionArrayEquation[i];
assortedAnswers[epsilon] = divisionArrayAnswers[i];
}
else{
assortedEquations[epsilon] = "Bonus: Enter 0";
}
}
}
/*Writing each equation to the file.*/
try {
BufferedWriter writeEquation = new BufferedWriter(new FileWriter("Equations.txt"));
BufferedWriter writeAnswer = new BufferedWriter(new FileWriter("Answers.txt"));
for(int theta = 0; theta < assortedEquations.length; theta++){
writeEquation.write(assortedEquations[theta]+ "\n");
writeAnswer.write(assortedAnswers[theta] + "\n");
}
writeEquation.close();
writeAnswer.close();
} catch (IOException e) {JOptionPane.showMessageDialog(null, "Whoops, there seems to have been an error. Sorry about that.");}
}
}
答案 0 :(得分:0)
如果不运行此功能,我认为您只是"Bonus: Enter 0"
?
if(Menu.subtractionBool = true)
if(Menu.subtractionBool == true)
这两行之间存在着天壤之别。
答案 1 :(得分:0)
我弄明白了这个问题。我的旧代码是:
int tempDivisionY = singleDigitNaturalGen.nextInt(11), tempDivisionX = singleDigitNaturalGen.nextInt(11);
double answer = ((double)tempDivisionY / (double)tempDivisionX);
String equation = "";
应该说:
String equation = tempDivisionY + " / " + tempDivisionX;