I'm extremely new when to comes to Java and programming in general. I am trying to create a simple program where you guess my age and if you are right it will say "correct" and if you are wrong it will say "wrong".
This is my code:
import java.util.InputMismatchException;
import java.util.Scanner; // This will import just the Scanner class.
public class GuessAge {
public static int main(int[] args) {
System.out.println("\nWhat is David's Age?");
Scanner userInputScanner = new Scanner(System.in);
int age = userInputScanner.nextLine();
int validInput = 20;
if (validInput == 20) {
return System.out.println("Correct!!");
}
else {
return System.out.println("Wrong....");
}
}
}
I get the error "incompatible types: void cannot be converted to int" but I have no void class in the code? I know my code is probably awful but if you guys could point me in the right direction that would be great. Thanks.
Your program does not have to return an int
in public static int main
. Instead you can have it as void
(meaning don't return anything). You should simply just print your statements and don't return
them. Also the int[]
should be String[]
and Scanner
should check for nextInt()
as pointed out in comments!
import java.util.InputMismatchException;
import java.util.Scanner; // This will import just the Scanner class.
public class GuessAge {
public static void main(String[] args) {
System.out.println("\nWhat is David's Age?");
Scanner userInputScanner = new Scanner(System.in);
int age = userInputScanner.nextInt();
int validInput = 20;
// typo in your code - compare to age
if (validInput == age) {
System.out.println("Correct!!");
}
else {
System.out.println("Wrong....");
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments