package kreki;
import java.util.Scanner;
public class Hungry {
public static void main(String [] args) {
String input;
char ans;
Scanner Hungry = new Scanner(System.in);
System.out.println("Are You Hungry?");
input = Hungry.next();
ans = input.charAt(0);
if(ans == "Y") {
System.out.println("Eat");
}else {
System.out.println("Starve");
}
Hungry.close();
}
}
I'm getting Type mismatch: cannot convert from char to String. How can I fix it? Thank you.
答案 0 :(得分:2)
It can be hard to spot for new in Java/programming overall.
Error is here:
if(ans == "Y") {
Change double quotes to single one
if(ans == 'Y') {
答案 1 :(得分:1)
In the if
:
if(ans == 'Y') {
System.out.println("Eat");
}
you just need so change to single quote, because those are used for char
symbols in Java. When you're using double quotes, Java is considering it a String
, therefore a mismatch of types on comparison.