如何在1个java文件中创建2个方法(乘法和乘法测试) [英] How do I create 2 methods in 1 java file (multiplication and multiplicationtest)

查看:63
本文介绍了如何在1个java文件中创建2个方法(乘法和乘法测试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个有助于学生学习乘法的程序。使用SecureRandom对象产生1到20之间的两个正整数。然后程序应该提示用户提出一个问题(使用哨兵控制的循环),例如:10次11多少?

然后学生输入答案。如果答案是正确的,则显示非常好的消息并提出另一个问题。如果答案是错误的,则显示消息否,请再试一次,让学生尝试相同的问题,直到学生最终做对。您必须创建两个java文件:Multiplication.java和MultiplicationTest.java。在乘法中添加两个方法:CreateQuestion和CheckResponse。在MultiplicationTest.java中,测试你的方法。



我尝试过:



我试图基本上做所有事情,但我的代码总是有问题。我解决了一件事,它弄乱了文件中的其他东西。这是我的乘法类代码。



import java.security.SecureRandom;

import java.util.Scanner;



public class Multiplication {

private final static SecureRandom myRandom = new SecureRandom(); //创建一个SecureRandom对象

private int num1;

private int num2;

private int answer;

private int guess;



public void createQuestion(){

扫描仪输入=新扫描仪(System.in);

num1 = 1 + myRandom.nextInt(20);

num2 = 1 + myRandom.nextInt(20);



回答= num1 * num2;

System.out.printf(什么是%d次%d,num1,num2);

guess = input.nextInt();

}

public String checkResponse(){

if(guess == answer){

System.out .println(那是对的);

}

else {

System.out.println(不。请再试一次! );

}

}

}

Write a program that that will help a student learn multiplication. Use SecureRandom object to produce two positive integers between 1 and 20. The program should then prompt the user with a question (use a sentinel-controlled loop), such as: How much is 10 times 11?
The student then inputs the answer. If the answer is correct display the message "very good" and ask another question. If the answer is wrong to display the message "no, please try again" and let the student try the same question until the student finally gets it right. You must create two java files: Multiplication.java and MultiplicationTest.java. In Multiplication add two methods: CreateQuestion and CheckResponse. In MultiplicationTest.java, test your method.

What I have tried:

I have tried to do basically everything, but there's always something wrong with my code. I fix one thing and it messes up other things in the file. Here's my code for Multiplication class.

import java.security.SecureRandom;
import java.util.Scanner;

public class Multiplication {
private final static SecureRandom myRandom = new SecureRandom (); //create a SecureRandom objec
private int num1;
private int num2;
private int answer;
private int guess;

public void createQuestion (){
Scanner input = new Scanner (System.in);
num1 = 1 + myRandom.nextInt(20);
num2 = 1 + myRandom.nextInt(20);

answer = num1 * num2;
System.out.printf("What is %d times %d", num1, num2);
guess = input.nextInt();
}
public String checkResponse (){
if (guess == answer){
System.out.println("That's correct");
}
else {
System.out.println("No. Please try again!");
}
}
}

推荐答案

引用:

我修复了一件事,它弄乱了文件中的其他内容。

I fix one thing and it messes up other things in the file.



寻求帮助时,最好用一个例子来解释问题是如何重现问题的。


When asking for help, it is a good idea to explain how things go wrong with an example to reproduce the problem.

引用:

我试图基本上做所有事情,但我的代码总是有问题。我修复了一件事,它弄乱了文件中的其他东西。

I have tried to do basically everything, but there's always something wrong with my code. I fix one thing and it messes up other things in the file.



可能是时候尝试调试器并观察你的代码执行了,它应该可以帮助你理解究竟发生了什么引擎盖后面。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与之比较它应该做什么。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是通过向你展示什么来帮助你正在进行。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

http:// docs。 oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java -application.html [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


May be it is time to try the debugger and watch your code performing, it should help you to understand what exactly happen behind the hood.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


尝试(我没有使用单独的文件进行测试。测试在主函数中完成):

Try (I've not used a separate file for testing. Testing is done in the main function):
import java.security.SecureRandom;
import java.util.Scanner;

public class Multiplication {
  private final static SecureRandom myRandom = new SecureRandom (); //create a SecureRandom objec
  private int answer;

  public String createQuestion ()
  {
    int num1 = 1 + myRandom.nextInt(20);
    int num2 = 1 + myRandom.nextInt(20);

    answer = num1 * num2;
    return String.format("What is %d times %d", num1, num2);
  }
  public boolean checkResponse (int guess)
  {
    return guess == answer;
  }

  public static void main( String args[] )
  {
    Scanner input = new Scanner (System.in);
    while ( true )
    {
      Multiplication mult = new Multiplication();
      String question = mult.createQuestion();
      boolean answerOK = false;
      do
      {
        System.out.println(question);
        int guess = input.nextInt();
        answerOK = mult.checkResponse(guess);
        if ( answerOK )
          System.out.println("That's correct");
        else
          System.out.println("No. Please try again!");
      } while ( ! answerOK );
    }
  }
}


这篇关于如何在1个java文件中创建2个方法(乘法和乘法测试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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