2014-03-27 79 views
0

我想要求用戶輸入yes或no,如果是yes,那麼它將重新啓動,並且會添加特徵,因爲它們會做出2個選擇。但是,它不會讓我問Y/N,而且我明顯在某個地方搞砸了。如果可能的話,我怎樣才能最大限度地減少代碼,而不是使用糟糕的優化語句。請幫忙,在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

你的代碼可以濃縮一些,但主要問題是yesno變量從未被初始化,所以有條件的if (yesno.equalsIgnoreCase("y"))計算結果爲假,以下塊從不計算,所以線

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

永遠不會被評估,用戶永遠不會被問到。如果將代碼保持原樣,則應在創建代碼時設置yesno="y"。但是,正如我所說的,您的代碼可以被簡化。