Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java在程序中使用多种方法_Java_Oop_Methods - Fatal编程技术网

Java在程序中使用多种方法

Java在程序中使用多种方法,java,oop,methods,Java,Oop,Methods,我有下面关于Java的代码,现在可以很好地工作了,但是我想知道如何使用多种方法(面向对象编程)编写相同的程序。现在所有的代码都在main方法中,我想让它至少有一个其他的方法来进行计算,这个方法将在main方法中调用。 该程序向用户询问不同类型的乘法问题,如2*3、9*5等 import java.util.Random; import javax.swing.JOptionPane; public class Main { public static void main(String[]

我有下面关于Java的代码,现在可以很好地工作了,但是我想知道如何使用多种方法(面向对象编程)编写相同的程序。现在所有的代码都在main方法中,我想让它至少有一个其他的方法来进行计算,这个方法将在main方法中调用。 该程序向用户询问不同类型的乘法问题,如2*3、9*5等

import java.util.Random;
import javax.swing.JOptionPane;

public class Main {

  public static void main(String[] args) {

    Random number = new Random();

    while (true) {

        int nmb1 = number.nextInt(10) + 1;
        int nmb2 = number.nextInt(10) + 1;
        int multi = nmb1 * nmb2;
        int question;

        // read the user's input ...
        do {
            question = Integer.parseInt(JOptionPane.showInputDialog("How much is" + nmb1 + "*" + nmb2));

            if (question != multi) {
                JOptionPane.showMessageDialog(null, "Wrong, try again");
            }
        }
        while (question != multi);
        // .. and repeat until the user types the correct answer

        JOptionPane.showMessageDialog(null, "Right");
      }
   }
}

如果有人能提供任何帮助,我将不胜感激。

您只需将您的计算和用户输入封装在方法调用乘法()中,如下面的代码所示。该方法将返回一个布尔值,以确定用户是否正确回答了问题。嗯

import java.util.Random;
import javax.swing.JOptionPane;

public class Main{

  public static void main(String[] args) {

      boolean correctAnswer;
      Random number = new Random();
      int nmb1;
      int nmb2;
      int multi;


    while (true) {
        nmb1 = number.nextInt(10) + 1;
        nmb2 = number.nextInt(10) + 1;
        multi = nmb1 * nmb2;

        // read the user's input ...
        do {
            correctAnswer = multiplication(nmb1,nmb2,multi);
        }
        while (correctAnswer != true);
        // .. and repeat until the user types the correct answer

        JOptionPane.showMessageDialog(null, "Right");
      }
   }


  public static boolean multiplication(int number1,int number2,int answer)
  {

      int question;

         question = Integer.parseInt(JOptionPane.showInputDialog("How much is" + number1 + "*" + number2));

          if (question != answer) {
              JOptionPane.showMessageDialog(null, "Wrong, try again");

              return false;
          }

      return true;

  }

}

这个问题不适合这样做。也许会更好,但我不知道他们的接受标准是什么。现在,这个问题很大程度上是“我如何用Java编写代码?”这个问题太宽泛了。如果这是学校作业,你应该咨询老师或同学。@jdv:如果你不知道代码评审的验收标准是什么,甚至不要建议。说真的,有太多不合适的东西放在那里,除非你确定,否则建议该网站只会导致更多不合适的东西放在那里。我不清楚你为什么要在这里使用多种方法。这似乎是很好的包含和清晰的,即使它运行在main中。@Makoto假装有人自己找不到代码复查,这是很愚蠢的。我明确建议将其作为进一步研究的资源。这整件“别提其他SE站点”的事情是荒谬的。谢谢你的帮助,但是现在如果给出错误的答案,程序不会重复同样的问题。我已经尝试过了,它确实重复了。你能再核实一下吗?我又贴了一遍,但还是不起作用。我正在使用EclipseMars4.5.0。好的,再次复制代码。我刚才对要求感到困惑。现在,如果答案是错误的,它应该重复同样的问题。