2013-09-29 5 views
0

저는 이것이 쉬운 질문이라는 것을 알고 있습니다. 그러나 저는 Java에 익숙하지 않고 잃어 버렸습니다. 내 프로그램에서 수행해야 할 모든 작업은 사용자가 예 또는 아니오를 입력하지 않고 프로그램이 끝나는 지점에서 "잘못된 입력, 다시 시도"와 같은 메시지를 출력합니다. 다른 계산을 요구합니다. 나는 그것이 초등학생이라는 것을 알고 있으며 내가 할 수있는 최선의 답변을 찾았지만 단순히 자바 용어에 대해 충분히 알지 못한다. 당신이 나를 도울 수 있다면 나는 그것을 많이 감사 할 것입니다!필요한 옵션을 입력하지 않은 경우 다른 입력을 요청하는 방법은 무엇입니까?

P.S. 내 변수가 대문자로 시작해서는 안된다는 것이 지적되었지만, 나는 미래에 그것을 인식하고 있으며 그렇게하지 않을 것입니다.

public class Main { 
    public static Scanner reader = new Scanner(System.in); 
    public static void main(String... args) { 
     // ... 
     switch(somthing) { 
     case "something": 
      // ... 
      if (!shallContinue()) return; 
      break; 
     case "something else": 
      // ... 
      if (!shallContinue()) return; 
      break; 
     default: 
      // ... 
      if (!shallContinue()) return; 
     } 
    } 

    public static boolean shallContinue() { 
     while (true) { 
      System.out.println("Do you want to continue?"); 
      switch (reader.nextLine().toLowerCase()) { 
      case "yes": 
       return true; 
      case "no": 
       return false; 
      } 
     } 
    } 
} 

Basicly 당신이 함수를 호출하고 방금 종료 결과에 따라 : 그것은

내가 당신에게 작은 예를 보여 드리겠습니다위한

System.out.println(" The purpose of this program is to calculate the speed of sound through several mediums.\n The program user will input a distance in feet followed by a mediumd and the program will output the speed in feet per second and miles per hour\n"); 
//declare variables 

Scanner keyboard = new Scanner(System.in); 

final double Air = 1126.1; 

final double Water = 4603.2; 

final double Steel = 20013.3; 

final double Earth = 22967.4; 

double OneFootPerSecond = .68181818182; 

double Distance; 

double AirSpeed; 

double WaterSpeed; 

double SteelSpeed; 

double EarthSpeed; 

boolean shouldContinue = true; 

while (shouldContinue == true){ 

System.out.print(" What is the distance in feet:"); 
//ask the user to input variables 


    while (!keyboard.hasNextDouble()){ 
    System.out.println("Please enter a valid numeric value, try again: "); 
    keyboard.next(); 
    } 
    Distance =keyboard.nextDouble(); 
    { 
    System.out.print("Input the media: Air, Water, Steel, or Earth: "); 
    String Input = keyboard.next(); 



    switch(Input.toLowerCase()) 

    { 

     case "air": 
     AirSpeed = Distance/Air; 
     System.out.print("\n \nThe time to for sound to travel "); 
     System.out.print(Distance); 
     System.out.print(" feet through AIR" +"\n"); 
     System.out.printf("%.6f", AirSpeed); 
     System.out.print(" seconds or "); 
     System.out.printf("%.1f", OneFootPerSecond*Air); 
     System.out.print(" miles per hour."); 
     System.out.print("\n \nEnter Yes for another calculation, else No: "); 
     String Another = keyboard.next(); 
     Another.toLowerCase(); 
     if (Another.equals("no")){ 
      shouldContinue = false; 
          } 
     if (!Another.equals("no")) 
      if (!Another.equals("yes")) 
      {System.out.print("Invalid."); 


      } 


     break; 



case "water": 
     WaterSpeed = Distance/Water; 
     System.out.print("\nThe time to for sound to travel "); 
     System.out.print(Distance); 
     System.out.print(" feet through WATER" +"\n"); 
     System.out.printf("%.6f",WaterSpeed); 
     System.out.print(" seconds or "); 
     System.out.printf("%.1f", OneFootPerSecond*Water); 
     System.out.print(" miles per hour."); 
     System.out.print("\n \nEnter Yes for another calculation, else No: "); 
     Another = keyboard.next(); 
     Another.toLowerCase(); 
     if (Another.equals("yes")){ 
      shouldContinue = false; 

     } 
break; 

case "steel": 
     SteelSpeed = Distance/Steel; 
     System.out.print("\nThe time to for sound to travel "); 
     System.out.print(Distance); 
     System.out.print(" feet through STEEL" +"\n"); 
     System.out.printf("%.6f",SteelSpeed); 
     System.out.print(" seconds or "); 
     System.out.printf("%.1f", OneFootPerSecond*Steel); 
     System.out.print(" miles per hour."); 
     System.out.print("\n \nEnter Yes for another calculation, else No: "); 
     Another = keyboard.next(); 
     Another.toLowerCase(); 
     if (Another.equals("yes")){ 
      shouldContinue = false; 

     } 
break;  

     case "earth": 
     EarthSpeed = Distance/Water; 
     System.out.print("\nThe time to for sound to travel "); 
     System.out.print(Distance); 
     System.out.print(" feet through EARTH" +"\n"); 
     System.out.printf("%.6f",EarthSpeed); 
     System.out.print(" seconds or "); 
     System.out.printf("%.1f", OneFootPerSecond*Earth); 
     System.out.print(" miles per hour."); 
     System.out.print("\n \nEnter Yes for another calculation, else No: "); 
     Another = keyboard.next(); 
     Another.toLowerCase(); 
     if (Another.equals("yes")){ 
      shouldContinue = false; 

     } 
break; 
default : 
     System.out.print("Invalid. Re-run the program. ");     
break;      
    } 
    } 
+0

GOTO LABEL (아니요, java는 이와 같이 사용하기위한 것이 아니며 메소드와 같은 멋진 객체를 가지고 있습니다. 코드를 별도의 메소드로 분리해야합니다. 함께 작업하기 쉽고 많은 시간을 절약 해줍니다.) – MightyPork

답변

0

예쁜 쉬운 솔루션은 작은 방법이 될 것입니다 . 그런 다음이 함수는 실제 사용자와 통신합니다.

관련 문제