2014-03-27 3 views
0

사용자에게 예 또는 아니오를 입력하도록 요청하는 중입니다. 그렇다면 다시 시작되고 특성이 추가됩니다. 그러나 그것은 내가 Y/N에게 물어 보지 못하게하고 분명히 어딘가에서 망쳐 놨습니다. 가능하다면 나는 그 눈이 좋지 않게 최적화 된 if 문을 사용하는 대신 코드를 최소화 할 수있는 방법은 무엇입니까?도와주세요. while 루프에서 뭔가 묻는 데 문제가 있습니다.

public static void main(String[]args) 
    { 
     //Number 1 
     Scanner scan = new Scanner(System.in); 
     int num = 0; 
     int num2 = 0; 
     int num3 = 0; 
     int count = 2; 
     String yesno; 
     do 
     { 
      System.out.println("Which aircraft would you like to simulate?"); 
      System.out.println("1. Blimp"); 
      System.out.println("2. Helicopter"); 
      System.out.println("3. Fighter Jet"); 
      System.out.println("4. Space Shuttle"); 
      num = scan.nextInt(); 
      if(num<1 || num>4) {System.out.println("Invalid");} 

     } 
     while(num<1 || num>4); 
     do 
     { 

      System.out.println("What characteristics would you like? (Input one, then the other)"); 
      System.out.println("1. Position Trim "); 
      System.out.println("2. Force Breakout"); 
      System.out.println("3. Force Gradient"); 
      System.out.println("4. Force Friction"); 
      System.out.println("5. Damping"); 
      System.out.println("6. Hard Stop"); 
      num2 = scan.nextInt(); 
      num3 = scan.nextInt(); 

      if(num2 == 1 || num3 == 1) 
      { 
       System.out.println("The position to which a flight control returns"); 
      } 
      if(num2 == 2 ||num3 == 2) 
      { 
       System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity)."); 
      } 
      if(num2 == 3 || num3 == 3) 
      { 
       System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim."); 
      } 
      if (num2 == 4 || num3 == 4) 
      { 
       System.out.println("A constant force that is opposite to the direction of movement"); 
      } 
      if(num2 == 5 || num3 == 5) 
      { 
       System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force."); 
      } 
      if (num2 == 6 || num3 == 6) 
      { 
       System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted"); 
      } 
      if(num2 < 1 || num2 > 6) 
      { 
       System.out.println("Invalid input"); 

      } 

      if (yesno.equalsIgnoreCase("y")) { 
       do 
       { 

        System.out.println("What characteristics would you like? (Input one, then the other)"); 
        System.out.println("1. Position Trim "); 
        System.out.println("2. Force Breakout"); 
        System.out.println("3. Force Gradient"); 
        System.out.println("4. Force Friction"); 
        System.out.println("5. Damping"); 
        System.out.println("6. Hard Stop"); 
        num2 = scan.nextInt(); 
        num3 = scan.nextInt(); 

        if(num2 == 1 || num3 == 1) 
        { 
         System.out.println("The position to which a flight control returns"); 
        } 
        if(num2 == 2 ||num3 == 2) 
        { 
         System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity)."); 
        } 
        if(num2 == 3 || num3 == 3) 
        { 
         System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim."); 
        } 
        if (num2 == 4 || num3 == 4) 
        { 
         System.out.println("A constant force that is opposite to the direction of movement"); 
        } 
        if(num2 == 5 || num3 == 5) 
        { 
         System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force."); 
        } 
        if (num2 == 6 || num3 == 6) 
        { 
         System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted"); 
        } 
        if(num2 < 1 || num2 > 6) 
        { 
         System.out.println("Invalid input"); 

        } 
        System.out.println("Would you like to re-select?(Y/N)"); 
        yesno = scan.nextLine(); 
        count += 2; 
       } 
       while(num2 < 1 || num2 > 6 || num3 < 1 ||num3 > 6); 

      } 
      else if (yesno.equalsIgnoreCase("n")) 
      { 
       System.out.println("You've used " + count + " characteristics"); 
      } 

     } 
     while(num2 < 1 || num2 > 6 || num3 < 1 ||num3 > 6); 

    } 
} 
+0

출력은 얼마나됩니까? –

+0

'yesno'를 읽거나 설정하려고하는 코드는 어디에 있습니까? 나는 줄에 문제가있는 것 같아요.'yesno'는 대기열에 남아있는 \ n을 읽습니다. – John3136

+0

내가 얻으려고하는 것은 예 또는 아니오라는 질문이고 특성 선택으로 돌아갑니다. – user3466743

답변

1

귀하의 코드 일부를 응축 될 수 있지만, 기본 문제는 조건 if (yesno.equalsIgnoreCase("y"))는 평가 결코 거짓과 다음 블록으로 평가하지 않도록 yesno 변수가 초기화되지 않습니다 것을, 그래서이 선

System.out.println("Would you like to re-select?(Y/N)"); 
    yesno = scan.nextLine(); 

결코 평가되지 않으며 사용자는 결코 묻지 않습니다. 코드를 그대로두면 작성시 yesno="y"으로 설정해야합니다. 하지만 내가 말했듯이 코드는 단순화 될 수 있습니다.

관련 문제