三元运算符,即时通讯试图获取用户输入的扫描仪结果,但不按照我的想法

问题描述

不了解为什么它无法正确读取

r

解决方法

您正在尝试将s用作ScannerintString

这是需要调整的部分:

s = s.nextInt();
s = i==i?light:heavy;

i==i也没有多大意义。这永远是真的。我猜你是说(whatever variable s.nextInt() will be assigned to)==i

,
Scanner s = new Scanner(System.in).useDelimiter("\r?\n");
System.out.println("How much do you weigh? 100 or 200 ?");
int a = s.nextInt();

String light;
String heavy;
light = (" You are light");
heavy = ("You are heavy");

String result = (a == 100) ? light : (a == 200) ? heavy : "not correct answer";

System.out.println(result);