2014-02-11 73 views
-2

我试着做一个循环,但“{”符号防止所有的打印行重复我认为。我希望循环在用户完成输入序列值后重新显示整个第一个字符串。循环 - 做,而

import java.util.Scanner; 

public class FtoC{ 

    static Scanner cin = new Scanner (System.in); 

    public static void main(String[] args) 
    { 
     char userInput = ' '; 
     System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius" 
         + "\nEnter " + "\"C\"" + " or " + "\"c\"" + " for Celsius to Fahrenheit" 
         + "\nEnter something else to quit." +"\n"); 


     userInput = cin.next().charAt(0); 

     if ((userInput == 'F') || (userInput =='f')) 
     {print("Enter a temp in Fahrenheit" 
       +"\n I will tell you temp in Celsius" +"\n"); 
      far2cel(); 
     } 
     else if ((userInput == 'C') || (userInput =='c')) { 
      print("Enter a temp in Celsius:" 
      +"\n I will tell you temp in fahrenheit" +"\n"); 
       cel2far(); 
     } else { 
      print("Terminating the program" + "\n"); 
     } 
    } 

    private static void cel2far() { 
     Scanner input = new Scanner(System.in); 
     Double celsius = input.nextDouble(); 
     print(celsius + " celsius is " + ((celsius * 9/5.0) + 32) 
       + " Fahrenheit"); 

    } 

    private static void far2cel() { 
     Scanner input = new Scanner(System.in); 

     Double Fahrenheit = input.nextDouble(); 
     print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5/9.0)) 
       + " celsius"); 

    } 

    private static void print(String string) { 
     System.out.print("\n" + string); 
    } 
} 
+0

我没有看到一个循环。你可以编辑这个来发布你试过的循环代码吗? – ajb

+3

您是否认真询问了一个do-while循环,而您的代码中没有单个循环?试试这个:https://www.google.com/search?q=java+do+while&oq=java+do+while – aliteralmind

+1

“''符号可以防止所有打印行重复”呃,不。有时间阅读旧的教程。 –

回答

0

我不认为我得到你想要什么,但这样一来就可以运行该程序,直到用户输入的东西比“F”,“F”,“C”,“C”,在不同的该计划的第一部分。

import java.util.Scanner; 

public class FtoC{ 

    static Scanner cin = new Scanner (System.in); 

    public static void main(String[] args) 
    { 
     while(true) { 
      char userInput = ' '; 
      System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius" 
          + "\nEnter " + "\"C\"" + " or " + "\"c\"" + " for Celsius to Fahrenheit" 
          + "\nEnter something else to quit." +"\n"); 

      userInput = cin.next().charAt(0); 

      if ((userInput == 'F') || (userInput =='f')) 
      {print("Enter a temp in Fahrenheit" 
        +"\n I will tell you temp in Celsius" +"\n"); 
       far2cel(); 
      } 
      else if ((userInput == 'C') || (userInput =='c')) { 
       print("Enter a temp in Celsius:" 
       +"\n I will tell you temp in fahrenheit" +"\n"); 
        cel2far(); 
      } else { 
       print("Terminating the program" + "\n"); 
       break; 
      } 
     } 
    } 

    private static void cel2far() { 
     Scanner input = new Scanner(System.in); 
     Double celsius = input.nextDouble(); 
     print(celsius + " celsius is " + ((celsius * 9/5.0) + 32) 
       + " Fahrenheit"); 

    } 

    private static void far2cel() { 
     Scanner input = new Scanner(System.in); 

     Double Fahrenheit = input.nextDouble(); 
     print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5/9.0)) 
       + " celsius"); 

    } 

    private static void print(String string) { 
     System.out.print("\n" + string); 
    } 
} 

我已将它放入while循环并添加“break”在“终止程序”部分。

+0

谢谢。有时最简单的解决方案是最好的。我一直试图做一个“尽可能”的声明,但那是行不通的。 – mo1090

+0

没问题,我很高兴它帮助你!感谢您选择我最好的答案,我真的很感激它。 – msmolcic