-2

我正在努力学习Java,但我有点挣扎。我正在尝试从教科书中做作业,所以我用 javascript 完成了它,并使用我对 Java 的有限知识来转换它。(这是原始说明 - http://i518.photobucket.com/albums/u341/ACrippledFerret/5ea8ec0e-02aa-4b96-b207-a83c52f8db48_zps2cd2ab7f.jpg

我认为我的大部分内容都是正确的,但我遇到了一些错误。几乎所有的都是“(变量)无法解析为变量”。在我的最后一种方法中,它发生在很多变量上。我似乎在某处也遇到了括号问题......“令牌“}”上的语法错误,{ 预计在此令牌之后”。

如果有人可以帮助修复此代码,我将不胜感激。我是 Java 新手,所以这对我来说翻译起来有点困难。第一组代码是javascript,第二组代码是我翻译的java(这不起作用)。谢谢你的帮助。

JAVASCRIPT

window.onload=function(){
var difficulty = 1,
    operators = ['plus', 'minus', 'times', 'divided by'],
    selectedOperator = 1,
    correctAnswers = 0,
    answeredTyped = 0;

var difficultyInput = parseInt(prompt('Please choose the difficulty. Enter the number of digits to use in each problem.'), 10);
if(difficultyInput > 0) {
    difficulty = difficultyInput;
}

var arithmeticMethod = parseInt(prompt('Choose an arithmetic problem to study:\n1 = Addition Only\n2 = Subtraction Only\n3 = Multiplication Only\n4 = Division Only\n5 = Random Problems'), 10);
if(arithmeticMethod == 5 || operators[arithmeticMethod - 1]) {
    selectedOperator = arithmeticMethod;
}

function checkResponse(primaryInt, secondaryInt, operatorText, suggestedAnswer) {
    var result = false;
    switch (operatorText) {
      case 'plus':
           return (primaryInt + secondaryInt) == suggestedAnswer;
      case 'minus':
           return (primaryInt - secondaryInt) == suggestedAnswer;
      case 'times':
           return (primaryInt * secondaryInt) == suggestedAnswer;
      case 'divided by':
           return (primaryInt / secondaryInt) == suggestedAnswer;
      default:
        return false;
    }
}

function displayResponse(isCorrect) {
    var randomIndex = 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.';
      default:
           return 'Woops...';
    }
}

function askQuestion() {
    var correctAnswer = false;
    var primaryInt = Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1));
    secondaryInt = Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1));
    operatorText = (selectedOperator == 5) ? operators[Math.floor(Math.random() * operators.length)] : operators[selectedOperator - 1];

    while(!correctAnswer && answeredTyped < 10) {
        var response = parseFloat(prompt('How much is ' + primaryInt + ' ' + operatorText + ' ' + secondaryInt + '?'));
        correctAnswer = checkResponse(primaryInt, secondaryInt, operatorText, response);
        alert(displayResponse(correctAnswer));

        answeredTyped++;

        if(correctAnswer)
            correctAnswers++;
    }
}

while(answeredTyped < 10) {
    askQuestion();
}

if((correctAnswers / answeredTyped) >= 0.75) {
    alert('Congratulations, you are ready to go on the next level!');
} else {
    alert('Please ask your teacher for extra help.');
}


}

JAVA

import java.util.*;
import javax.swing.JOptionPane;
/**
 * 
 */

/**
 * @author Tyler
 *
 */
public class Assignment2 {

    /**
     * @param args
     */
    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;

    }




    public static boolean checkResponse (double primaryInt, double 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;
        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];

        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.");
    }
}
}
}
4

1 回答 1

0

Java 中的作用域与 Javascript 不同;difficulty在方法(静态或实例)中定义的 Java 变量仅在该方法内有效,因此您不能main()从其他方法(例如askQuestion(). 您可以将它们设为静态类成员变量,但这在 Java 中通常是不好的做法(实际上在 Javascript 中也是如此),因此更好的选择是将它们作为参数传递给 askQuestion() 等方法。

于 2013-09-28T03:49:26.520 回答