尝试将Javascript转换为Java-help修复程序?
我正在努力学习Java,但我有点挣扎。我试图做一个教科书上的作业,所以我用javascript做了,并用我有限的Java知识将它转换过来。(以下是原始说明-) 我想我大部分都是对的,但我遇到了几个错误。几乎所有这些都是“(变量)无法解析为变量”。在我的上一个方法中,它发生在很多变量上。我似乎也有一个括号的问题某处。。。“标记“}”上的语法错误,{应在该标记之后” 如果有人能帮我修复这段代码,我会非常感激。我是Java新手,所以这段代码对我来说有点难翻译。第一组代码是javascript,第二组代码是我翻译的Java(不起作用)。谢谢你的帮助 JAVASCRIPT尝试将Javascript转换为Java-help修复程序?,java,javascript,Java,Javascript,我正在努力学习Java,但我有点挣扎。我试图做一个教科书上的作业,所以我用javascript做了,并用我有限的Java知识将它转换过来。(以下是原始说明-) 我想我大部分都是对的,但我遇到了几个错误。几乎所有这些都是“(变量)无法解析为变量”。在我的上一个方法中,它发生在很多变量上。我似乎也有一个括号的问题某处。。。“标记“}”上的语法错误,{应在该标记之后” 如果有人能帮我修复这段代码,我会非常感激。我是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.');
}
}
window.onload=function(){
var难度=1,
运算符=[‘加’、‘减’、‘乘’、‘除’],
选择运算符=1,
正确答案=0,
回答类型=0;
var-difficultyInput=parseInt(提示('请选择难度。输入每个问题要使用的位数'),10);
如果(困难输入>0){
困难=困难输入;
}
var算术方法=parseInt(提示符('选择要研究的算术问题:\n1=仅添加\n2=仅减法\n3=仅乘法\n4=仅除法\n5=随机问题'),10);
if(算术方法==5 | |运算符[算术方法-1]){
选择运算符=算术方法;
}
函数检查响应(primaryInt、secondaryInt、operatorText、suggestedAnswer){
var结果=假;
开关(操作员文本){
案例“附加”:
返回(primaryInt+secondaryInt)=建议的答案;
减号:
返回(primaryInt-secondaryInt)=建议的答案;
案例“时代”:
返回(primaryInt*secondaryInt)=建议的回答;
案例“除以”:
返回(primaryInt/secondaryInt)=建议的回答;
违约:
返回false;
}
}
功能显示响应(isCorrect){
var randomIndex=Math.floor(Math.random()*(4-1+1))+1;
开关(随机索引){
案例1:
return是否正确?“非常好!”:“不,请再试一次。”;
案例2:
return是正确的吗?“太好了!”:“错了,再试一次。”;
案例3:
回答正确吗?“干得好!”:“不要放弃!”;
案例4:
回答正确吗?“继续努力!”:“不,继续努力。”;
违约:
返回“Woops…”;
}
}
函数askQuestion(){
var correctAnswer=假;
var primaryInt=Math.floor(Math.pow(10,难度-1)+Math.random()*9*Math.pow(10,难度-1));
secondaryInt=Math.floor(Math.pow(10,难度-1)+Math.random()*9*Math.pow(10,难度-1));
operatorText=(selectedOperator==5)?运算符[Math.floor(Math.random()*operators.length)]:运算符[selectedOperator-1];
而(!正确答案和回答键入<10){
var response=parseFloat(提示(“多少是'+primaryInt+''+operatorText+''+secondaryInt+'?'));
correctAnswer=checkResponse(primaryInt、secondaryInt、operatorText、response);
警报(显示响应(正确应答));
回答输入++;
如果(正确答案)
正确答案++;
}
}
while(回答类型<10){
askQuestion();
}
如果((正确答案/回答类型)>=0.75){
警惕(“祝贺你,你已经准备好进入下一个级别了!”);
}否则{
警惕('请向老师寻求额外帮助');
}
}
爪哇
import java.util.*;
导入javax.swing.JOptionPane;
/**
*
*/
/**
*@作者泰勒
*
*/
公共课堂作业2{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪输入=新扫描仪(System.in);
智力难度=1;
字符串[]运算符={“加”、“减”、“倍”、“除以”};
int-selectedOperator=1;
int=0;
int-answeredTyped=0;
int-difficultyInput=Integer.parseInt(JOptionPane.showInputDialog(“请选择难度,输入每个问题要使用的位数”);
如果(困难输入>0){
困难=困难输入;
}
int arithmaticmethod=Integer.parseInt(JOptionPane.showInputDialog(“选择要研究的算术问题:1=仅加法,2=仅减法,3=仅乘法,4=仅除法,5=随机问题”);
选择运算符=算术方法;
}
公共静态布尔checkResponse(双primaryInt、双secondaryInt、字符串运算符text、浮点响应){
布尔结果=假;
开关(操作员文本){
案例“1”:
返回(primaryInt+secondaryInt)=响应;
案例“2”:
返回(primaryInt-secondaryInt)=响应;
案例“3”:
返回(primaryInt*secondaryInt)=响应;
案例“4”:
返回(primaryInt/secondaryInt)=响应;
}
返回false;
}
公共静态字符串displayResponse(布尔值isCorrect){
intrandomIndex=(int)(Math.floor(Math.random()*(4-1+1))+1);
开关(随机索引){
案例1:
返回正确吗?“非常好!”:“不,请再试一次。”;
案例2:
return是否正确?“太好了!”:“错了,再试一次。”;
案例3:
回答正确吗?“干得好!”:“不要放弃!”;
案例4:
回答正确吗?“继续努力!”:“不,继续努力。”;
}
返回“Oops…”;
}
公共静态void askQuestion(int难度、字符串运算符或文本、int selectedOperator、int answeredTyped、字符串[]运算符、int更正答案){
布尔正确答案=假;
double primaryInt=Math.floor(Math.pow(10,难度-1)+Math.random()*9*Math.pow(10,难度-1));
蠢货
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.");
}
}
}
}