2016-07-26 2 views
-1

私の目標は正解の量を 'correctOutOf'メソッドに格納し、例の/*Wrapping Up*/セクションで呼び出されたときにcorrAnsの値を返します以下のコード:java:method littleQuizクラスのcorrectOutOfは、指定された型には適用できません

import java.util.Scanner; 

public class littleQuiz { 

    public static void main(String[] args){ 

     Scanner key = new Scanner(System.in); 

     char yesno; 
     int answer; 

     /*Welcome/Splash Screen*/ 
     /*Ask if ready and accept yes or no with appropriate return response*/ 
     System.out.print("Are you ready for a quiz? Y or N "); 
     yesno = key.next().charAt(0); 

     if (yesno == 'Y'){ 
      /*affirmative response*/ 
      System.out.println("Okay, here it comes!"); 
     } 
     else{ 
      /*negative response*/ 
      System.out.println("What a wimp..."); 
      System.exit(0); 
     } 

     /*Quiz Section*/ 

     /*Question 1*/ 
     System.out.println("Q1) What is the capital of Alaska?"); 
     System.out.println("   1) Melbourne\n" + 
         "   2) Anchorage\n" + 
         "   3) Juneau"); 
     answer = key.nextInt(); 

     if (answer == 3){ 
      System.out.println("\nCorrect!!!"); 
      /*store to function stating number of correct answers*/ 
     } 
     else{ 
      System.out.println("\nWrong."); 
     } 

     /*Question 2*/ 
     System.out.println("Q2) Can you store the value 'cat' in a variable of type int?"); 
     System.out.println("   1) yes\n" + 
          "   2) no"); 
     answer = key.nextInt(); 

     if (answer == 2){ 
      System.out.println("\nCorrect!!!"); 
      /*store to function stating number of correct answers*/ 
     } 
     else{ 
      System.out.println("\nWrong."); 
     } 

     /*Question 3*/ 
     System.out.println("Q3) What is the result of 9+6/3?"); 
     System.out.println("   1) 5\n" + 
          "   2) 11\n" + 
          "   3) 15/3"); 
     answer = key.nextInt(); 

     if (answer == 2){ 
      System.out.println("\nCorrect!!!"); 
      /*store to function stating number of correct answers*/ 
     } 
     else{ 
      System.out.println("\nWrong."); 
     } 

     /*Wrapping Up*/ 
     System.out.println("Overall, you got " + correctOutOf() + " out of 3 correct."); 
     System.out.println("Thanks for playing!"); 
    } 

    /*not sure of which access modifier to use, but none have fixed it*/ 
    private static int correctOutOf(int answer) { 
     return corrAns; 
    } 
} 

私は私のif文は、それがのようにコードで正解かどうかを確認することができ文の一部に過ぎないという理由だけで「correctOutOf」メソッドを養うために起こっていることはかなりポジティブ感じています-is。 (誰もが私の思考の訓練を知っているので)。

編集 - これは初心者以上の人がいらっしゃいます。 (私はかむことができるよりも多くをオフに噛み?)

+0

は、パラメータはありませんどのように多くのあなた'correctOutOf'メソッドは宣言しますか? –

+0

「int answer」は私が必要と思う唯一のものです。変数 'int answer'を最後の手段として渡してみました。 –

+0

これは、1つのパラメータが大きいと期待しています。今あなたの呼び出しでいくつの引数 'correctOutOf()'が渡されていますか? –

答えて

1

それをしないでください、メインの開始時に次の操作を行います。あなたは、メインの外にそれを使用する必要がある場合

byte correct = 0; 

またはこの:

correct++; 

を、変数 "正しい" を印刷:

private static byte correct = 0; 

Thenステートメント場合、各正解にこれを追加します。

...また、あなたは、「key.nextIntを()」置き換えるためにあなたのプログラムクラッシュからユーザーを防ぐために、あなたのプログラムにこの機能を追加することがあります。

import java.util.regex.*; 

private static final int integer() { 
    boolean invalid = true; 
    int number = 0; 
    while (invalid) { 
     String input = key.next(); 
     if (input.matches("\\d+")){ 
      invalid = false; 
      try { 
       number = Int.parseInt(input); 
      } catch (java.lang.NumberFormatException e) { 
       invalid = true; 
       System.out.print("Are you trying to break the program? Try again: "); 
      } 
     } else { 
      System.out.println("That's not a whole number! "); 
      System.out.print("Try again: "); 
     } 
    } 
    return number; 
} 
+0

私は反復の基礎を理解しましたか?多分? –

+0

私はあなたが何を意味するか正確には分かりません。このコードはまったくループなしで全く線形です。同じコード行を何度か繰り返すのを除いて、ここではあまり反復しません。 –

+0

'++'?気にしないで。私は実際に最後の2〜3年の間に単に触れている以外のものから始めています。 増分/減算は、私が使っていたと思った単語です。 –

関連する問題