Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向现有java程序添加循环?_Java_Loops_While Loop_Do While_Repeat - Fatal编程技术网

向现有java程序添加循环?

向现有java程序添加循环?,java,loops,while-loop,do-while,repeat,Java,Loops,While Loop,Do While,Repeat,我需要修改我的程序,以便在需要时可以多次运行它。如果用户输入Q或Q,我需要退出程序,如果输入的不是请求的输入或退出命令,则问题将重复。 以下是我目前掌握的代码: import java.util.Scanner; public class TemperatureLoop { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args)

我需要修改我的程序,以便在需要时可以多次运行它。如果用户输入Q或Q,我需要退出程序,如果输入的不是请求的输入或退出命令,则问题将重复。 以下是我目前掌握的代码:

import java.util.Scanner;

public class TemperatureLoop
{

    private static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) 
    {

        System.out.println("Enter a temperature in degrees (for example 32.6): ");
        double temp;
        temp = keyboard.nextDouble();
        System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
        String letter = keyboard.next();
        double total = 0;
        //if Farenheit then do this equation
        if (letter.equals("F") || (letter.equals("f")))
        {
            total = ((temp-32)*5)/9; //convert the entered temperature to Celsius
            System.out.println(temp + " degrees F = " + total + " degrees Celsius");
        }
        else //if Celsius then do this
        if (letter.equals("C") || (letter.equals("c")) )
        {
            total = (((temp*9))/5)+32; //convert the entered temperature to Farenheit
            System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");
        }
    }
}
在这种情况下,应该使用do while循环

String letter = "";
do{
  System.out.println("Enter a temperature in degrees (for example 32.6): ");
  double temp = 0;
  while(true){
      if(keyboard.hasNextDouble())
      {
          temp = keyboard.nextDouble();
          break;
      }
      else
      {
          System.out.println("Enter a valid double");
          sc.nextLine();
      }
  }
  System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
  letter = keyboard.next();
  double total = 0;
  //if Farenheit then do this equation
  if (letter.equalsIgnoreCase("F"))
  {
      total = ((temp-32)*5)/9; //convert the entered temperature to Celsius
      System.out.println(temp + " degrees F = " + total + " degrees Celsius");
  }
  else if (letter.equalsIgnoreCase("C"))
  {   //if Celsius then do this
      total = (((temp*9))/5)+32; //convert the entered temperature to Farenheit
      System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");
  }
}while(!letter.equalsIgnoreCase("Q"));
循环的工作原理是,do部分中的任何内容都将至少执行一次。然后它将检查while条件以确定是否再次执行do部分。正如您所说,一旦用户输入Q或Q,程序将结束,因为while条件将计算为false,do部分将不再执行。因此,在这种情况下,循环将终止


当你输入Q或Q时会发生什么?do部分在技术上会发生,但是if语句将被忽略,因为它不满足这些条件。一旦达到while检查,条件将计算为false,从而导致循环结束。如果您输入了类似于M或g的内容,则If语句将被忽略,但循环不会结束,因为while条件不会计算为false,因此程序将再次询问您温度和度数。

我建议将您拥有的内容放入while循环中,如果用户输入“Q”或“Q”,循环将中断。类似于以下内容:

// Declare your breaking condition variable outside the while loop
boolean done = false;
while (!done){
   //  Your existing code here
   //  A conditional to check for 'Q' or 'q'
   //  set done to true if the above line evaluates as true.
}

因此,使用do while循环,当用户输入Q时,它将退出/终止程序?我刚看了你的编辑,非常有用,谢谢。我曾有人告诉我,我需要将整个程序嵌套在至少三个while循环中,以使其正常运行,我不知道为什么它能如此好地工作。@Tony更正,因为如果用户输入Q或Q,那么程序将忽略if语句,一旦循环条件得到评估,循环将结束,因为字母是一个值,它将导致计算结果为false。出于某种原因,我正在使用Eclipse的IDE告诉我,它无法解析while条件中的变量字母。“这是我的IDE的问题还是我遗漏了什么?”Tony我明白了,在循环之外定义字母。检查我的编辑,解决了那个问题。如果用户为程序输入了错误值,如果出现错误并终止程序,我希望为用户重复问题的代码类型是什么?