java:LittleQuike类中的correctOutOf方法无法应用于给定类型

java:LittleQuike类中的correctOutOf方法无法应用于给定类型,java,methods,Java,Methods,我的目标是将正确答案的数量存储到“correctOutOf”方法中,然后在调用下面示例代码的/*Wrapping*/部分时,该方法将返回corrAns的值: import java.util.Scanner; public class littleQuiz { public static void main(String[] args){ Scanner key = new Scanner(System.in); char yesno;

我的目标是将正确答案的数量存储到“correctOutOf”方法中,然后在调用下面示例代码的
/*Wrapping*/
部分时,该方法将返回corrAns的值:

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'方法,因为它是语句中唯一可以按原样检查代码是否正确的部分。(这样每个人都知道我的思路。)


编辑-如果这不是初学者应该处理的事情,谢谢你指出。(咬得比我能嚼的还多?

不要这样做,在main开始时这样做:

byte correct = 0;
或者,如果您需要在main之外使用它:

private static byte correct = 0;
然后将以下内容添加到每个正确答案if语句中:

correct++;
并打印变量“correct”

…此外,您可能希望将此函数添加到程序中,以替换“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; 
} 

您的
correctOutOf
方法声明了多少个参数?
int-answer
是我唯一需要的参数。作为最后手段,我尝试传递变量
int-answer
,但没有任何积极结果。它需要一个参数,太好了。现在您在调用中传递了多少个参数,
correctOutOf()
?不确定。。。完成noob,但是-
int result=answer++再使用两个变量,一个用于totalQuestions,另一个用于correctAnswerCount。提问时,增加总问题数;回答正确时,增加正确答案数。修改coorrectoutof方法并将这两个变量传递给它。谢谢aaron。我刚刚了解了迭代的基本原理?也许吧?我不太清楚你的意思。这段代码是完全线性的,没有任何循环。除了可能重复几次同一行代码外,这里没有太多的迭代。
++
?没有关系。在过去的2-3年里,我真的只是刚刚开始,而不是简单的修修补补。增量/减量是我想使用的单词。++函数只添加了一个。这与“正确+=1”或“正确=正确+1”相同;声誉低于15,否则我会。我会在我能做的时候再回来:)