2016-11-26 4 views
0

私は割り当てのための簡単な数学プログラムを作成しようとしています。私は1時間以上それを費やしましたが、私は式を正しく検証するようには思えません。他のすべてがうまくいくようです。問題は、正しい数の質問を検証する時間が来たときに、最後の数だけがカウントされることです。 3分の3の権利があれば、3分の1が正しいと言います。私が最初の2つの権利と2番目の間違いを得た場合、3つのうち0が正しいと言います。ここで クラスの初心者で数学の問題を確認することができません

は私のコードです:

import java.util.Scanner; 
public class ElementaryMath { 

    private double num1; 
    private double num2; 
    private String opType; 
    private int numCorrect; 
    private int actualAns; 

    ElementaryMath(double n1, double n2, String ot){ 
     num1=n1; 
     num2=n2; 
     opType=ot; 
    } 

    public void printQuestion(){ 
     System.out.println((int)num1 + " " + opType + " " + (int)num2); 
    } 

    public void checkAnswer(int ans, String op){ 

     if(op.equals("/")) { 
      actualAns = (int) num1/(int) num2; 
      System.out.println("answer is " + actualAns +" "+ ans); 
     } 
     if(op.equals("*")){ 
      actualAns = (int) num1 * (int) num2; 
      System.out.println("answer is " + actualAns + " " + ans); 
     } 

     if(actualAns == ans){ 
      numCorrect++; 
     } 

    } 

    public String numCorr(){ 
     return ""+numCorrect+""; 
    } 

    public static void main(String[]args){ 
     Scanner in = new Scanner(System.in); 
     double n1, n2; 
     String op = ""; 
     int nq, qt, aq; 

     System.out.println("Which option would you like?"); 
     System.out.println("1. Single Digit - One operation"); 
     System.out.println("2. Two Digit - One operation."); 
     System.out.println("3. Single Digit - Multiple Operations (Mixed operators)"); 
     System.out.println(""); 
     System.out.println("Please enter your option. (1, 2, or 3): "); 
     qt = in.nextInt(); 
     if(qt==1 || qt==2) { 
      System.out.println("Please enter which operator you would like to use: (type/or *)"); 
      op = in.next(); 
     } 
     System.out.println("Enter the number of questions you would like: "); 
     nq = in.nextInt(); 

     if(qt==1 || qt==2) 
     { 
     for(int i=0;i<nq;i++) { 
      n1 = Math.random() * 9 + 1; 
      n2 = Math.random() * 9 + 1; 
      ElementaryMath a = new ElementaryMath(n1, n2, op); 
      a.printQuestion(); 
      System.out.println("Please print your answer: "); 
      aq = in.nextInt(); 
      a.checkAnswer(aq, op); 
      if(i==(nq-1)) { 
       System.out.println("You have successfully answered " + a.numCorr() + " out of " + nq + " questions correctly."); 
       } 
      } 
     } 
    } 
} 
+0

ループの各繰り返しで、* new * 'ElementaryMath'オブジェクトの作成を続けます。それぞれがnumCorrect値0で始まります。あなたのデザインを再考する必要があります。 – Andreas

答えて

1

問題は静的カウンタ変数(numCorrect)が必要だったことです。次に、与えられた答えと答えが正しいかどうかを確認する必要がありました。ここに変更されたコードがあります。

import java.util.Scanner; 
public class ElementaryMath { 

    private double num1; 
    private double num2; 
    private String opType; 
    private static int numCorrect; 
    private int actualAns; 

    ElementaryMath(double n1, double n2, String ot){ 
     num1=n1; 
     num2=n2; 
     opType=ot; 
    } 

    public void printQuestion(){ 
     System.out.println((int)num1 + " " + opType + " " + (int)num2); 
    } 

    public void checkAnswer(int ans, String op){ 

     if(op.equals("/")) { 
      actualAns = (int) num1/(int) num2; 
      System.out.println("answer is " + actualAns +" "+ ans); 
      if(actualAns == ans) 
      { 
       numCorrect++; 
      } 
     } 
     if(op.equals("*")){ 
      actualAns = (int) num1 * (int) num2; 
      System.out.println("answer is " + actualAns + " " + ans); 
      if(actualAns == ans) 
      { 
       numCorrect++; 
      } 
     } 
    } 

    public String numCorr(){ 
     return ""+numCorrect+""; 
    } 

    public static void main(String[]args){ 
     Scanner in = new Scanner(System.in); 
     double n1, n2; 
     String op = ""; 
     int nq, qt, aq; 

     System.out.println("Which option would you like?"); 
     System.out.println("1. Single Digit - One operation"); 
     System.out.println("2. Two Digit - One operation."); 
     System.out.println("3. Single Digit - Multiple Operations (Mixed operators)"); 
     System.out.println(""); 
     System.out.println("Please enter your option. (1, 2, or 3): "); 
     qt = in.nextInt(); 
     if(qt==1 || qt==2) { 
      System.out.println("Please enter which operator you would like to use: (type/or *)"); 
      op = in.next(); 
     } 
     System.out.println("Enter the number of questions you would like: "); 
     nq = in.nextInt(); 

     if(qt==1 || qt==2) 
     { 
      for(int i=0;i<nq;i++) { 
       n1 = Math.random() * 9 + 1; 
       n2 = Math.random() * 9 + 1; 
       ElementaryMath a = new ElementaryMath(n1, n2, op); 
       a.printQuestion(); 
       System.out.println("Please print your answer: "); 
       aq = in.nextInt(); 
       a.checkAnswer(aq, op); 
       if(i==(nq-1)) { 
        System.out.println("You have successfully answered " + numCorrect + " out of " + nq + " questions correctly."); 
       } 
      } 
     } 
    } 
} 

これは、誤っているなどの問題を修正します。

+0

すでに解決済みですが、これは私が学ぶことができるもう一つの取り組みです。あなたのご意見ありがとうございます! – asdfghjklm

+0

私は助けてくれるとうれしいよ:) – Aaron

1

問題は、あなたのElementaryMathオブジェクトのインスタンス変数に正解の数を維持しているが、あなたはElementaryMathの新しいインスタンスを作成し(それゆえされていることです新しいカウンター)forループを回るたびに。

+0

質問を最初に生成する必要がある時間を知らなくても、クラスの新しいインスタンスを生成するループを作成する方法はわかりません。 – asdfghjklm

+0

たとえば、任意の数の質問を生成する必要があります(明らかに理由の中で) – asdfghjklm

+0

変数をnumcorrectに変更してmainメソッドをチェックし、checkAnsメソッドをtrueの真偽値を返すように変更しました偽であるかどうかをチェックすることができます。 – asdfghjklm

関連する問題