在 else if 语句内部使用 input.equals 不按预期工作。

huangapple 未分类评论46阅读模式
英文:

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(&quot;temperatures.txt&quot;);
        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 &amp;&amp; count &gt; 0){
            for (int i = 0; i &lt; len;i++){
                System.out.println(count+&quot; Cities temperature left!&quot;);
                System.out.print(&quot;Do you want to check temperature details; enter y or n: &quot;);
                ans = input.nextLine();
                if(ans.equals(&quot;y&quot;)){
                    String[] getData = myReader.nextLine().split(&quot; &quot;);
                    System.out.println(getData[0]);
                    System.out.println(&quot;************&quot;);
                    String[] days = {&quot;Mon&quot;, &quot;Tue&quot;, &quot;Weds&quot;, &quot;Thurs&quot;, &quot;Fri&quot;, &quot;Sat&quot;, &quot;Sun&quot;};
                    int[] temp = new int[days.length];
                    for (int j = 0; j &lt; days.length; j++){
                        System.out.println(days[j]+&quot;: &quot;+getData[j + 1]);
                        temp[j] = Integer.parseInt(getData[j + 1]);
                    }
                    System.out.println(&quot;The average temperature: &quot;+avgTemp(temp)+&quot;*C&quot;);
                    System.out.println(&quot;The warmest temperature is: &quot;+warmestTemp(temp)+&quot;*C&quot;);
                    System.out.println(&quot;The coldest temperature is: &quot;+coldestTemp(temp)+&quot;*C&quot;);
                    System.out.println(&quot;The average temperature excluding coldest and warmest is: &quot;+avgTempWithoutTwoExtremes(temp)+&quot;*C&quot;);
                    System.out.println(&quot;*********************&quot;);
                    count--;
                }
                else if(ans.equals(&quot;n&quot;)){
                    stop = true;
                }
                else{
                    System.out.println(&quot;invalid input&quot;);
                    stop = true;
                }
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println(&quot;An error occurred.&quot;);
        e.printStackTrace();
  }

huangapple
  • 本文由 发表于 2020年4月11日 05:26:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61148932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定