英文:
input.equals inside an else if statement doesn't work as expected
问题
以下是您提供的代码的翻译部分:
我有一个 if 语句,用于检查用户的输入,当输入等于 "yes" 时,while 循环会运行;如果输入等于 "no",则 while 循环停止。if 条件正常工作,但 else if 部分不按预期工作。
try {
File myObj = new File("temperatures.txt");
FileReader read = new FileReader(myObj);
Scanner myReader = new Scanner(myObj);
Scanner input = new Scanner(System.in);
LineNumberReader lines = new LineNumberReader(read);
lines.skip(Long.MAX_VALUE);
int len = lines.getLineNumber();
int count = len;
String ans;
boolean stop = false;
while (!stop && count > 0) {
for (int i = 0; i < len; i++) {
System.out.println(count + " 城市剩余温度!");
System.out.print("是否要查看温度详情?输入 y 或 n:");
ans = input.nextLine();
if (ans.equals("y")) {
String[] getData = myReader.nextLine().split(" ");
System.out.println(getData[0]);
System.out.println("************");
String[] days = {"Mon", "Tue", "Weds", "Thurs", "Fri", "Sat", "Sun"};
int[] temp = new int[days.length];
for (int j = 0; j < days.length; j++) {
System.out.println(days[j] + ": " + getData[j + 1]);
temp[j] = Integer.parseInt(getData[j + 1]);
}
System.out.println("平均温度:" + avgTemp(temp) + "°C");
System.out.println("最高温度:" + warmestTemp(temp) + "°C");
System.out.println("最低温度:" + coldestTemp(temp) + "°C");
System.out.println("排除最高和最低后的平均温度:" + avgTempWithoutTwoExtremes(temp) + "°C");
System.out.println("*********************");
count--;
} else if (ans.equals("n")) {
stop = true;
} else {
System.out.println("无效的输入");
stop = true;
}
}
}
} catch (FileNotFoundException e) {
System.out.println("发生错误。");
e.printStackTrace();
}
英文:
i have an if statement that checks the input of a user, while the input is = "yes" the while loop runs if the input is "no" then the while loop stops, the if condition works properly but the else if doesn't work as expected.
try {
File myObj = new File("temperatures.txt");
FileReader read = new FileReader(myObj);
Scanner myReader = new Scanner(myObj);
Scanner input = new Scanner(System.in);
LineNumberReader lines = new LineNumberReader(read);
lines.skip(Long.MAX_VALUE);
int len = lines.getLineNumber();
int count = len;
String ans;
boolean stop = false;
while(!stop && count > 0){
for (int i = 0; i < len;i++){
System.out.println(count+" Cities temperature left!");
System.out.print("Do you want to check temperature details; enter y or n: ");
ans = input.nextLine();
if(ans.equals("y")){
String[] getData = myReader.nextLine().split(" ");
System.out.println(getData[0]);
System.out.println("************");
String[] days = {"Mon", "Tue", "Weds", "Thurs", "Fri", "Sat", "Sun"};
int[] temp = new int[days.length];
for (int j = 0; j < days.length; j++){
System.out.println(days[j]+": "+getData[j + 1]);
temp[j] = Integer.parseInt(getData[j + 1]);
}
System.out.println("The average temperature: "+avgTemp(temp)+"*C");
System.out.println("The warmest temperature is: "+warmestTemp(temp)+"*C");
System.out.println("The coldest temperature is: "+coldestTemp(temp)+"*C");
System.out.println("The average temperature excluding coldest and warmest is: "+avgTempWithoutTwoExtremes(temp)+"*C");
System.out.println("*********************");
count--;
}
else if(ans.equals("n")){
stop = true;
}
else{
System.out.println("invalid input");
stop = true;
}
}
}
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论