私の目標は正解の量を '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。 (誰もが私の思考の訓練を知っているので)。
編集 - これは初心者以上の人がいらっしゃいます。 (私はかむことができるよりも多くをオフに噛み?)
は、パラメータはありませんどのように多くのあなた'correctOutOf'メソッドは宣言しますか? –
「int answer」は私が必要と思う唯一のものです。変数 'int answer'を最後の手段として渡してみました。 –
これは、1つのパラメータが大きいと期待しています。今あなたの呼び出しでいくつの引数 'correctOutOf()'が渡されていますか? –