2014-02-11 3 views
-2

나는 루프를 만들려고했지만 "{"기호는 모든 인쇄 줄이 반복되는 것을 방지합니다. 사용자가 시퀀스 값을 입력 한 후 루프가 전체 첫 번째 문자열을 다시 표시하도록합니다.Loop - Do, while

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

감사합니다. 때로는 가장 간단한 솔루션이 가장 좋습니다. 나는 "do while"진술에 노력했지만 계속 작동하지는 않았다. – mo1090

+0

문제 없습니다. 도움이 되었기 때문에 기쁩니다! 최고의 답변을 주셔서 고마워요, 정말 고마워요. – msmolcic