我试图让用户输入是或否(是或否),但我一直遇到并发症。我还必须使小写 y 和 n 起作用。我想知道最好的方法是什么?这是我的程序:import java.util.Scanner;public class Homework2 { public static void main(String args[]) { System.out.println("\nWould you like to see some healthy weight loss guidelines? (Y or N)"); char decision = (char) System.in.read(); if (decision.equals("Y")) { System.out.println("\nPlaceholder"); } else if (decision.equals("N")) { System.out.println("\n"); System.out.println("========================================================"); System.out.println("|| ||"); System.out.println("|| Thank you From Your Friends At ||"); System.out.println("|| Happy Valley Fitness Center! ||"); System.out.println("|| ||"); System.out.println("========================================================"); } else { System.out.println("\nYou did not enter yes or no, program stopping"); System.out.println("\n"); System.out.println("========================================================"); System.out.println("|| ||"); System.out.println("|| Thank you From Your Friends At ||"); System.out.println("|| Happy Valley Fitness Center! ||"); System.out.println("|| ||"); System.out.println("========================================================"); } }}
1 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
眼下的问题是,你不能叫.equals()上char。而是简单地从System.ina读取String并使用该方法,.equalsIgnoreCase()因此它将匹配大写的 Y/N 或小写的 y/n:
Scanner in = new Scanner(System.in);
String decision = in.next();
if(decision.equalsIgnoreCase("Y"))
{
System.out.println("\nPlaceholder");
}
添加回答
举报
0/150
提交
取消