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
Java 不确定要使用什么循环_Java_Loops_For Loop_While Loop - Fatal编程技术网

Java 不确定要使用什么循环

Java 不确定要使用什么循环,java,loops,for-loop,while-loop,Java,Loops,For Loop,While Loop,编辑:我只是想对所有帮助过我的人说声谢谢!我是Java新手,我的教授向外界推荐了这个网站。到目前为止一切都很好。我将花一些时间编写代码并实现我收到的建议。谢谢 我试图创建一个程序,将温度从华氏度转换为摄氏度,反之亦然。如果用户没有输入正确的单位,程序将返回并要求他们重新输入单位或输入Q退出程序。这就是我到目前为止所做的: 我的想法是将一个循环合并到switch语句的默认部分,但我不确定使用哪个循环(可能是为了?)将循环并要求用户重新输入单元或退出。任何帮助都是非常非常感谢的!我已经被困在这几个小

编辑:我只是想对所有帮助过我的人说声谢谢!我是Java新手,我的教授向外界推荐了这个网站。到目前为止一切都很好。我将花一些时间编写代码并实现我收到的建议。谢谢

我试图创建一个程序,将温度从华氏度转换为摄氏度,反之亦然。如果用户没有输入正确的单位,程序将返回并要求他们重新输入单位或输入Q退出程序。这就是我到目前为止所做的:

我的想法是将一个循环合并到switch语句的默认部分,但我不确定使用哪个循环(可能是为了?)将循环并要求用户重新输入单元或退出。任何帮助都是非常非常感谢的!我已经被困在这几个小时了

System.out.print("\tEnter 'F' (or 'f') for Fahrenheit or " 
            + "'C' (or 'c') for Celcius: ");
f_or_c = console.next();

double fahrenheit = 5*(temperature-32)/9;
double celcius = (9*(temperature)/5)+32;

switch (f_or_c) {
    case "C":
        case "c":
        System.out.println("\n\t" + temperature + " " + " degrees Celcius "
        + "= " + celcius + " degrees Fahrenheit.");
        break;
    case "F":
        case "f":
        System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit "
        + "= " + fahrenheit + " degrees Celcius.");
        break;
    default: 
        System.out.println("\n\tUnknown units -"
        + "\n\tCannot do calculation -"
        + "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius");
        System.out.print("\nEnter 'Q' to quit or " +
    "any other character to perform another temperature conversion: ");
       break;

你可以用while循环。有更好的方法可以做到这一点。只是帮你写代码而已

 System.out.print("\tEnter 'F' (or 'f') for Fahrenheit or " 
        + "'C' (or 'c') for Celcius: ");
 f_or_c = console.next();
 boolean flag=false;
 double fahrenheit = 5*(temperature-32)/9;
 double celcius = (9*(temperature)/5)+32;
 while(true) {
  switch (f_or_c) {
      case "C":
      case "c":
           System.out.println("\n\t" + temperature + " " + " degrees Celcius "
            + "= " + celcius + " degrees Fahrenheit.");
           flag=false;
           break;
     case "F":
     case "f":
           System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit "
             + "= " + fahrenheit + " degrees Celcius.");
           flag=false;
           break;
     default: 
           System.out.println("\n\tUnknown units -"
           + "\n\tCannot do calculation -"
           + "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius");
           System.out.print("\nEnter 'Q' to quit or " +
          "any other character to perform another temperature conversion: ");
          flag=true;
          break;
     }
    if(flag) 
        continue;
    break;
 }

让我们从逻辑上考虑一下:您希望循环,直到用户输入正确的输入。那么,这是否意味着您将在开始循环之前知道循环应该迭代多少次?这是关键

一旦知道此信息,正确的循环就会出现,因为:

  • 当您提前知道要循环多少次时,将使用for循环
  • 而循环则用于您事先不知道这些信息的情况

    • 将switch语句放入一个方法中,然后在默认情况下调用该方法本身。它使用的是递归,但请记住,如果将来使用递归,请确保它不会无限循环,否则会出现堆栈溢出错误。

      使用递归如何

      基本上,我们在开始时将
      badInput
      设置为false,假设它是良好的输入。如果我们最终得到错误的输入,我们会使
      badInput
      true,因此我们将继续循环

      do-while
      while
      循环之间的一般区别在于
      do-while
      s用于至少要执行一次的代码。在这种情况下,它工作得很好,因为您知道您希望至少读取一次输入


      在我的示例中,变量
      badInput
      称为a。基本上,标志只是一个变量,用于跟踪某个对象是“开”还是“关”。在这种情况下,
      badInput
      正在跟踪是否存在错误输入。这是一个简单的“开”“关”“真”“假”场景。

      我可能会改变您的思维方式,但我会以不同的方式实现它。在开关盒上绕一圈。这将确保使用下一个用户输入值重新执行开关案例。我已经包括了一个它可能看起来像什么的例子。请注意,我没有包括输入代码,因为有许多选项供您选择。此外,我还没有包括退出代码,它只是打破了循环

      bool repeat = TRUE;
      while (repeat)
      {
       switch (f_or_c) 
       {
        case "C":
            case "c":
            System.out.println("\n\t" + temperature + " " + " degrees Celcius "
            + "= " + celcius + " degrees Fahrenheit.");
            repeat = FALSE;
            break;
        case "F":
           case "f":
           System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit "
           + "= " + fahrenheit + " degrees Celcius.");
           repeat = FALSE;
           break;
        case "Q":
           ***YOUR CODE TO QUIT***
           repeat = FALSE;
           break;
        default: 
           System.out.println("\n\tUnknown units -"
           + "\n\tCannot do calculation -"
           + "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius");
           System.out.print("\nEnter 'Q' to quit or " +
           "any other character to perform another temperature conversion: ");
           ***CODE TO PROMPT USER FOR  F_OR_C VALUE***
           repeat = TRUE;
           break; 
        }
      }
      

      是的,你说得对!我们不知道循环将循环多少次。何时结束循环完全取决于用户,因此while循环听起来不错!将while循环嵌套在switch语句的默认值中似乎明智吗?或者我应该创建while循环并在其中嵌套switch语句吗?谢谢大家!@user3470441:循环应该包含您希望重复调用的任何代码/逻辑。所以开关会进入while循环。我确实想过要做一些事情,但我的教授希望我们提交一个.java文件,所以我认为最好将它全部保存在这个程序中。谢谢。他也提到了单类,但多方法。你实际上在寻找一种叫做循环的东西。我试着在我的回答中解释一下它是如何工作的。真的很抱歉迟了回复。我能够找到它,并获得代码,以教授希望的方式使用嵌套while循环执行。我确实看了一下您的do-while循环示例,因为了解更多不会有什么坏处。我将尝试使用do-while循环方法制作另一个副本。谢谢,我真的在考虑这样做。正如另一位用户指出的那样,我意识到将开关嵌套在while循环中可能更有效。非常感谢。我将尝试重写代码,使开关嵌套在while中。建议在switch语句周围包装一个while循环,效果很好!谢谢大家!
      bool repeat = TRUE;
      while (repeat)
      {
       switch (f_or_c) 
       {
        case "C":
            case "c":
            System.out.println("\n\t" + temperature + " " + " degrees Celcius "
            + "= " + celcius + " degrees Fahrenheit.");
            repeat = FALSE;
            break;
        case "F":
           case "f":
           System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit "
           + "= " + fahrenheit + " degrees Celcius.");
           repeat = FALSE;
           break;
        case "Q":
           ***YOUR CODE TO QUIT***
           repeat = FALSE;
           break;
        default: 
           System.out.println("\n\tUnknown units -"
           + "\n\tCannot do calculation -"
           + "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius");
           System.out.print("\nEnter 'Q' to quit or " +
           "any other character to perform another temperature conversion: ");
           ***CODE TO PROMPT USER FOR  F_OR_C VALUE***
           repeat = TRUE;
           break; 
        }
      }