如何显示用户对两个随机整数乘积的答案的正确答案?

时间:2017-03-20 21:45:10

标签: java methods

我想知道如何在方法"Your answer is wrong. The correct answer is "的{​​{1}}循环中显示for之后立即显示正确的答案。我的问题是答案的变量在main()方法之前没有被声明。

oneProblem()

5 个答案:

答案 0 :(得分:1)

您需要找到一种方法来返回状态(布尔值)和oneProblem()方法的答案。

这样做的一种方法是使用抽象的问题"每种问题的类和继承。

还有一些不错的设计模式。

问题类看起来像:

public class problem  {
    private string answer; // could be of an abstract parent class
    private boolean isTestPassed = false; // could be of an abstract parent class

    private (sometype) solution; // this one of concrete class

    public void askQuestion() { // could be defined at abstract parent class level
        // compute solution
        ...
        this.solution = // computed solution

        // ask question and get answer
        ...
        this.answer = // put user input in here

        this.isTestPassed = (this.answer.equals(this.solution));
    }
    public getAnsswer(); // could be defined at abstract parent class level
    public getSolution(); // could be defined at abstract parent class level
    public getIsTestPassed(); // could be defined at abstract parent class level
}

有了这个,您可以创建任何类型的问题,并且可以实现尽可能多的问题。 您可以通过方便的方法获得用户答案和解决方案。

您可以定义列表,表格或任何类型的问题集合,以改进或扩展您的软件。

当然,正如其他答案所示,您可以通过无限量的方式打印用户输入。请参阅@ Shashwat的回答

答案 1 :(得分:0)

鉴于这是一个测验,我假设您只是尝试一次提出1个问题,如果是这种情况,您可以将随机变量保存在类范围

public class SomeClass{
private int ran1;
private int ran2;

public booleans somequiz(int in)
 ran1 = random.nextInt();
ran2 = random.nextInt();
}

这意味着在任何给定时间都可以访问当前的测验随机数,直到再次调用测验函数并且它们被新的随机int覆盖,但是确保在使用之前在某个点分配了整数它们或它将返回一个nullpointer

public class SomeClass{
private int a;
  public static main(String[] args){
   System.out.Print(a); // a was never set thus nullpointer
  }
}

答案 2 :(得分:0)

oneProblem方法本身

中添加日志
static boolean oneProblem(int qno) {
    int number1 = 1 + rand.nextInt(10); 
    int number2 = 1 + rand.nextInt(10);        

    out.print("\nProblem " + qno + ": What is " + number1 + " times " 
              + number2 + "? ");        
    int answer = cin.nextInt();

    if (answer == number1 * number2) {
       out.println("Your answer is correct!");
       return true;
    }else {
        out.println("Your answer is wrong. The correct answer is " + (number1*number2));
        return false;
    }
}

答案 3 :(得分:0)

static方法之外设置main变量:

public class Main {

final static Scanner cin = new Scanner(System.in);
final static Random rand = new Random();
static int ans = 0;

现在,在oneProblem方法中,添加: -

ans = number1 * number2; //Add this Line
if (answer == number1 * number2)
    return true;
else
    return false;

现在, correctAnswer 存储在ans中。现在你可以简单地打印出来:

out.println("Your answer is wrong. The correct answer is " + ans);

答案 4 :(得分:0)

oneProblem可以返回结果Object而不是boolean。

class result {
  public result(boolean s, int n) { status = s; number = n; }
  public boolean status;
  public int number;
}

static result oneProblem(int qno) {
  int number1 = 1 + rand.nextInt(10), number2 = 1 + rand.nextInt(10);        

  out.print("\nProblem " + qno + ": What is " + number1 + " times " 
          + number2 + "? ");        
  int answer = cin.nextInt();

  if (answer == number1 * number2)
      return new result(true, number1 * number2);
  else 
      return new result(false, number1 * number2);
}

当你想检查时:

result res = oneProblem(qno);
if (res.status == true){
  out.println("Your answer is correct!");
  numCorrect ++;
}
else
  out.println("Your answer is wrong. The correct answer is " + res.number);