Ja-oon Solo Ja-oon Solo - 4 months ago 6
Java Question

java: method correctOutOf in class littleQuiz cannot be applied to given types

My goal is to store the amount of correct answers into the 'correctOutOf' method, which will then return the value of corrAns when called int the

/*Wrapping Up*/
section of the example code below:

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;
}
}


I'm feeling pretty positive that my if statement is going to feed the 'correctOutOf' method simply because it is the only part of the statement that can check for a correct answer with the code as-is. (just so everyone knows my train of thought.)

Edit - if this is something more than a beginner should be messing with, thanks for pointing it out. (Biting off more than I can chew?)

Answer

Don't do that, do this at the start of main:

byte correct = 0;

Or this if you ever need to use it outside of main:

private static byte correct = 0;

Then add this to each correct answer if statement:

correct++;

And print the variable "correct".

...also, you may wish to add this function to your program, to replace "key.nextInt()", to prevent the user from crashing your program:

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; 
}