- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 Java 数学培训程序。我有一个 Javascript 的工作版本,你们中的一些人已经帮助我将它转换为 Java。它应该问用户一个困难(然后对问题中的每个数字使用该数量的数字)。然后它会询问用户要进行哪种类型的数学运算(加法、减法、乘法、除法、随机)。然后它会询问用户 10 个问题。当用户回答时,它会告诉他们是对还是错。如果错了,他们可以继续尝试这个问题。在 10 个问题结束时,它会计算您是否答对了超过 75%,并显示适当的回答。完整说明:
我终于让它的大部分工作正常,却发现数学本身是错误的。有时如果我输入2的难度,它只给出1位(它基本上不会计算正确的难度)。此外,它总是告诉我我的数学是错误的。你们有没有机会发现逻辑上的任何错误?
感谢您的帮助。
import java.util.*;
import javax.swing.JOptionPane;
public class Assignment2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int difficulty = 1;
String[] operators = {"plus", "minus", "times", "divided by"};
int selectedOperator = 1;
int correctAnswers = 0;
int answeredTyped = 0;
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, arithmeticMethod,
arithmeticMethod, operators, arithmeticMethod);
}
public static boolean checkResponse (
int primaryInt, int secondaryInt,
String operatorText, float response){
boolean result = false;
switch (operatorText) {
case "1":
return (primaryInt + secondaryInt) == response;
case "2":
return (primaryInt - secondaryInt) == response;
case "3":
return (primaryInt * secondaryInt) == response;
case "4":
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;
int primaryInt = (int) Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1));
int secondaryInt = (int) 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];
while(!correctAnswer && answeredTyped < 10) {
float response = Float.parseFloat (JOptionPane.showInputDialog("How much is " + primaryInt + " " + operatorText + " " + secondaryInt + "?"));
correctAnswer = checkResponse (primaryInt, secondaryInt, operatorText, response);
JOptionPane.showMessageDialog(null, displayResponse(correctAnswer));
answeredTyped++;
if(correctAnswer)
correctAnswers++;
}
{
while(answeredTyped < 10) {
askQuestion(0, null, 0, 0, null, 0);
}
if((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.");
}
}
}
}
最佳答案
在您调用 askQuestion 的代码中您已完成
new Assignment2().askQuestion(arithmeticMethod, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod);
}
但是看看你的方法定义,你传递的是 arthmeticMethod 而不是难度级别
new Assignment2().askQuestion(difficulty, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod);
}
关于askQuestion() 方法中的 Java 逻辑问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063312/
我正在尝试创建一个 Java 数学培训程序。我有一个 Javascript 的工作版本,你们中的一些人已经帮助我将它转换为 Java。它应该问用户一个困难(然后对问题中的每个数字使用该数量的数字)。然
我在 CodePen 中的笔的链接 - http://codepen.io/PartTimeCoder/pen/WwMxEX?editors=0010 Javascript 在这里: function
我不想创建带有输入的表单。 你可以在里面输入你想要的,你也可以很容易地删除它。 就像 stackoverflow 的提问标签 block 一样。 我该怎么办! what should i do 最佳答
Tkinter中messagebox的askquestion()和askyesno()函数有什么区别? 我在这个网站上找到了这两个函数:http://infohost.nmt.edu/tcc/help
我是一名优秀的程序员,十分优秀!