2017-02-15 1 views
1

何か助けやアドバイスをいただければ幸いです。私は10種類のランダムな質問を生成する簡単なゲームをしています。質問は、2,3または4の整数から構成できます。このようなもの:552 − 4 − 101102/3/3589 − 281123 + 56 + 2数式の値を計算してユーザーの回答を確認するにはどうすればよいですか?

質問はテキストビューに表示され、ユーザーは推測して編集テキストに値を入力し、カスタムキーパッドのキーをクリックすると回答を確認してから次の質問を表示しますそれは10の質問に達する。私は持っているコードから答えを代入することに問題があります。私がここで何をしていても、ランダムに生成された式への答えは入力できません。で

public enum Operator { 

PLUS("+"), MINUS("-"), MULTIPLIER("*"), DIVIDER("/"); 
private String displayValue; 

private Operator(String displayValue) { 
    this.displayValue = displayValue; 
} 
public String getDisplayValue() { 
    return displayValue; 
}} 




public class Question{ 

private List<QuestionElement> questionElements; 

public Question(int sizeOfQuestionElemets) { 
    questionElements = new ArrayList<QuestionElement>(sizeOfQuestionElemets); 
} 


public void addElement(QuestionElement questionElement) { 
    questionElements.add(questionElement); 
} 

public List<QuestionElement> getElements() { 
    return questionElements; 
} 

public int size() { 
    return questionElements.size(); 
} 

@Override 
public String toString() { 
    StringBuilder sb = new StringBuilder(); 
    for (QuestionElement questionElement : questionElements) { 
     sb.append(questionElement); 
    } 
    return sb.toString().trim(); 
} 
} 

public class QuestionElement { 

private int value; 
private Operator operator; 

public int getValue() { 
    return value; 
} 

public void setValue(int value) { 
    this.value = value; 
} 

public Operator getOperator() { 
    return operator; 
} 

public void setOperator(Operator operator) { 
    this.operator = operator; 
} 

@Override 
public String toString() { 
    return value + (operator == null ? "" : " " + operator.getDisplayValue()) + " "; 
} 
} 

public class RandomQuestions { 


static QuestionElement q = new QuestionElement(); 
private static final int NUMBER_OF_QUESTIONS = 10; 
private static final int MIN_QUESTION_ELEMENTS = 2; 
private static final int MAX_QUESTION_ELEMENTS = 2; 
private static final int MIN_QUESTION_ELEMENT_VALUE = 1; 
private static final int MAX_QUESTION_ELEMENT_VALUE = 20; 
private final Random randomGenerator = new Random(); 




public List<Question> getGeneratedRandomQuestions() { 
    List<Question> randomQuestions = new ArrayList<>(NUMBER_OF_QUESTIONS); 
    int randomQuestionElementsCapacity = getRandomQuestionElementsCapacity(); 
    Question question = new Question(randomQuestionElementsCapacity); 
    for (int j = 0; j < randomQuestionElementsCapacity; j++) { 
     boolean isLastIteration = j + 1 == randomQuestionElementsCapacity; 

     QuestionElement questionElement = new QuestionElement(); 
     questionElement.setValue(getRandomQuestionElementValue()); 
     questionElement.setOperator(isLastIteration ? null 
       : Operator.values()[randomGenerator.nextInt(Operator.values().length)]); 

     question.addElement(questionElement); 
    } 
    randomQuestions.add(question); 

    return randomQuestions; 
} 

private int getRandomQuestionElementsCapacity() { 
    return getRandomIntegerFromRange(MIN_QUESTION_ELEMENTS, MAX_QUESTION_ELEMENTS); 
} 

private int getRandomQuestionElementValue() { 
    return getRandomIntegerFromRange(MIN_QUESTION_ELEMENT_VALUE, MAX_QUESTION_ELEMENT_VALUE); 
} 

private int getRandomIntegerFromRange(int min, int max) { 
    return randomGenerator.nextInt(max - min + 1) + min; 
} 





public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    RandomQuestions questionGenerator = new RandomQuestions(); 
    List<Question> randomQuestions = questionGenerator.getGeneratedRandomQuestions(); 
    for (Question question : randomQuestions) { 
     System.out.println(""+ question+"=?"); 
     int answer = input.nextInt(); 

     if (answer == q.getValue()) { 
      System.out.println("CORRECT"); 
     }else{ 
      System.err.println("STILL NOT WORKING"); 
     } 

    } 
} 
} 
+0

次回に質問を投稿するときは、問題をより正確に説明してください。そうすれば、読者は多くのコードを読んで、それがどのように機能していないのかを知ることができます。 [良い質問をするにはどうすればいいですか?](http://stackoverflow.com/help/how-to-ask)を参照してください。そして、あなたのコードを書くのではなく、私たちがあなたの質問に答えることを一般的に期待しています。あなたの周りを見て欲しい。 –

答えて

2

main()ユーザーからの回答を読んで、questionを印刷し、その後q.getValue()への回答を比較しています。 qは、questionに関連しない質問要素であり、常に値0を持っています。だから、そのトリックは0という質問に関係なく答えることです。プログラムはCORRECTを印刷します。 :-)

計算式の正しい値を計算しているコードのどこにも見つかりませんでした。これはおそらく、ユーザーが実際に正しい結果を入力したかどうかを確認するための最初のステップになるでしょう。

演算子の優先順位を考慮する必要がある場合は、正しい結果を計算することは実際には簡単ではありません。 4 + 3 * 2は10でなければなりません(14ではありません)。私は約the Shunting-yard algorithmの読書はあなたのいくつかの方法を得る必要がありますと信じています。 のアルゴリズムの計算式です。これは数値を計算するための最初のステップですが、依然として最初のステップです。

私は、オブジェクト指向のアプローチは、答えをチェックする方法を知っているQuestionオブジェクトであることをお勧めします。ここでは4つの事業者に簡略化されたアルゴリズムの実装は、ですが、実際に計算を行うように拡張:

public boolean checkAnswer(int answer) { 
    // calculate correct answer 
    // use shunting yard algorithm 
    Deque<Integer> outputQueue = new ArrayDeque<>(); 
    Deque<Operator> operatorStack = new ArrayDeque<>(); 
    for (QuestionElement element : questionElements) { 
     outputQueue.push(element.getValue()); 
     Operator op = element.getOperator(); 
     if (op != null) { 
      while (!operatorStack.isEmpty() && op.getPrecedence() <= operatorStack.peek().getPrecedence()) { 
       int operand2 = outputQueue.pop(); 
       int operand1 = outputQueue.pop(); 
       outputQueue.push(operatorStack.pop().apply(operand1, operand2)); 
      } 
      operatorStack.push(op); 
     } 
    } 
    while (!operatorStack.isEmpty()) { 
     int operand2 = outputQueue.pop(); 
     int operand1 = outputQueue.pop(); 
     outputQueue.push(operatorStack.pop().apply(operand1, operand2)); 
    } 
    int result = outputQueue.pop(); 
    assert outputQueue.isEmpty(); 

    return answer == result; 
} 

あなたは、私はあまりにもあなたのOperator列挙型のいくつかの新しい需要を入れていることに気づきます。それが優先されます。そして+オペレータは、(そのapplyメソッドを介して)追加を行う方法を知って、そして同様に他の事業者のための必要があります。上の

PLUS("+", 1) { 
    @Override 
    public int apply(int operand1, int operand2) { 
     return operand1 + operand2; 
    } 
}, 
// etc. 

public abstract int apply(int operand1, int operand2); 

ので。 1が優先です。 */は、実施例2のために、高い優先順位を持つ

main()であなただけ記述する必要があります。

 if (question.checkAnswer(answer)) { 

あなたは厳密に左から右への評価が適用されていることをユーザーに説明することを決定した場合、

public boolean checkAnswer(int answer) { 
    // calculate correct answer 
    // do left to right calculation 
    int result = questionElements.get(0).getValue(); 
    for (int elementIndex = 1; elementIndex < questionElements.size(); elementIndex++) { 
     Operator op = questionElements.get(elementIndex - 1).getOperator(); 
     result = op.apply(result, questionElements.get(elementIndex).getValue()); 
    } 

    return answer == result; 
} 

事業者はまだapplyメソッドを持っている必要がありますが、彼らはもはや優先を必要としない:やや単純なってきました。

+0

それは私がここにある問題です、どうすれば正しい結果が得られますか? –

+0

はい、これは右に計算されます。何とかお手伝いできますか?どのように私はこのlolを計算するのですか –

+0

あなたはおそらく、質問とMVCEを上記のコメントとして残しておき、回答から混乱を取り除いてください。 –

関連する問題