用随机数教授有人乘法的程序 [英] Program That Teaches Someone Multiplication with Random Numbers

查看:80
本文介绍了用随机数教授有人乘法的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个教授某人乘法的程序(游戏)。在这个程序中,将生成随机的数字对并插入到问题中:什么是x * y = z?如果此人正确回答,则系统将打印出非常好!如果此人没有正确回答问题,系统将打印出否则请再试一次。 (作为回报,该程序将继续提问,直到该人正确回答问题。)当该人正确回答问题时,新方法将产生另一个问题供该人回答。

I am writing a program (game) that teaches someone multiplication. In this program, random pair of numbers are to be generated and inserted into the question: "What is x * y = z?" If the person answers correctly, then the system will print out "Very Good!" If the person does not answer the question correctly, then the system will print out, "No. Please try again." (Which, in return, the program will continue to ask the question until the person answers the question correctly.) As the person answers the question correctly, a new method will generate another question for the person to answer.

我的代码在变量answer和if语句(猜测!=答案)中断。

My code is breaking at the variable "answer" and the if statement "(guess != answer)."

这是我的代码:

 public class Exercise_535 {

     public class Multiply{
         SecureRandom randomNumbers = new SecureRandom();

         int answer;
     }

     public void Quiz() {

         Scanner input = new Scanner(System.in);
         int guess;

         System.out.println("Enter your answer: ");
         guess = input.nextInt();

         while(guess != -1)
                 {
                     checkResponse(guess);

                     System.out.println("Enter your answer: ");
                     guess = input.nextInt();  
                 }

     }

     public void createQuestion(){

         SecureRandom randomNumbers = new SecureRandom();

         int digit1 = randomNumbers.nextInt();
         int digit2 = randomNumbers.nextInt();

         answer = digit1 * digit2;

         System.out.printf("How much is %d times %d\n", digit1, digit2);

     }

     public void checkResponse(){

     if (guess != answer)

         System.out.println("No. Please try again.");

     else{
         System.out.print("Very Good!");

         createQuestion();
     }


 }

 }

有没有人能够提供帮助,或者至少指出我正确的方向?

Is there anyone that is able to help, or at least point me in the correct direction?

谢谢。

推荐答案

理解变量的范围很重要。在java中,如果你创建一个变量(你说int guess或int answer),那个变量只存在于你放入的任何花括号中 - > {}。因此,如果您需要在另一个方法中使用该变量,则需要在括号中将该变量传递给它。 checkResponse()不知道猜测或答案是什么,因为它们没有在该范围内声明,并且你没有在函数的开头传入它们(你可以使用checkResponse(int guess,int answer)和然后在你调用它时传递它们,例如)。

It is important to understand the scope of variables. In java if you create a variable (where you say int guess or int answer) that variable only lives within whatever curly braces you put it in -> { }. So if you need that variable in another method, you need to pass it that variable in the parentheses. checkResponse() doesn't know what guess or answer are, because they aren't declared in that scope, and you don't pass them in at the start of the function (you could have checkResponse(int guess, int answer) and then pass those in when you call it, for example).

你有一个内部类Multiply,你有没有理由在类中创建一个类?有理由这样做,但似乎你没有任何理由这样做。

You have an inner class Multiply, is there a reason you created a class within a class? There are reasons to do that, but it doesn't seem like you have any reason to do that here.

此外我没有看到主要功能,这是Java程序的入口点,需要从那里调用所有其他函数(因此Main()可以在你的情况下调用Quiz(),然后调用你的其他两个函数)。计算机一次读取一行程序,当你调用一个函数/方法(如Quiz())时,它会跳转到该部分,然后在该函数调用return时返回。

Also I don't see a main function, which is the entry point to a Java program, and all other functions need to be called from there (so Main() could then call Quiz() in your case, which would then call your other two functions). Computers read programs one line at a time, and when you call a function/method (like Quiz()) it jumps to that part, and then returns when that function calls "return".

我知道这是很多信息,但您似乎并不了解Java程序的流程。你用什么来学习Java?如果您正在阅读一本书或开设一门课程,我建议您先阅读一些早期的课程,以便更好地了解该课程的流程。人们很难回答您的问题,因为您的代码设置方式没有逻辑流程(这就是为什么它不起作用)。希望这会有所帮助。

I know this is a lot of information, but it doesn't seem like you understand how Java programs flow. What are you using to study Java? If you are reading a book or doing a course, I recommend reviewing some of the earlier lessons, to understand the flow of the program better. It is difficult for people to answer your question because the way your code is set up doesn't have a logical flow (which is why it isn't working). Hope this helps a little.

这篇关于用随机数教授有人乘法的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆