且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不兼容的类型:void不能转换为int

更新时间:2023-02-18 20:59:59

您的程序不必在 int c $ c> public static int main 。相反,你可以有 void (意味着不返回任何东西)。你应该只打印你的语句,不要 return 。此外, int [] 应为 String [] 扫描程序应该检查注释中指出的 nextInt()

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....");
    }
}

}