2014-08-30 46 views
0

我無法讓while循環工作。程序提示用戶是否要繼續,輸入0 toquit或任何其他整數繼續。如果用戶想繼續操作,程序將循環回到菜單,否則它將退出。因此,你應該有一個標記控制的循環,當進入我的哨兵控制while循環不工作

import java.util.Scanner; 

public class AdoptAPet 
{ 
    public static void main(String[] args) 
    { 
    Scanner input = new Scanner(System.in); 
    int gender = 0; 

    double costAdoption = 0.0; 
    double costGenderSelect = 0.0; 
    double costAnyGender = 0.0; 
    double costProcessing = 0.0; 
    double total = 0.0; 
    int genderCounter = 0; 

    System.out.println("Choose from the following options: 1 for male, 2 for female, 3 for any"); 

    while(gender != 0);//begin sentinel controlled while loop 
    { 
    System.out.print("Please choose the gender of the cat you wish to purchase:"); 
    gender = input.nextInt(); 

    if(gender < 0) 
    { 
    System.out.println("INVALID CHOICE! "); 

    System.out.print("enter 0 to quit: 0 or enter any other nunber to continue:"); 
    gender = input.nextInt(); 
    } 

    if(gender == 1) 
    { 
    costAdoption = 9.50; 
    costAdoption += costProcessing + 2.00; 
    total += costAdoption; 

    System.out.printf("Your Choice is: "); 
    System.out.printf("male and the total is $%.2f", total); 
    } 

    if(gender == 2) 
    { 
    costGenderSelect = 11.50; 
    costGenderSelect += costProcessing + 2.00; 
    total += costGenderSelect; 

    System.out.printf("Your Choice is: "); 
    System.out.printf("female and the total is $%.2f", total); 
    } 

    if(gender == 3) 
    { 
    costAnyGender = 9.50; 
    costAnyGender += costProcessing + 2.00; 
    total += costAnyGender; 

    System.out.printf("Your Choice is: "); 
    System.out.printf("Any gender and the total is $%.2f", total); 
    } 

    } 

    } 
} 

回答

0

這條線負責一個0,將結束:

while(gender != 0);//begin sentinel controlled while loop 

仔細查看。最後有一個分號 - 這是整個陳述。接下來的{}塊不屬於循環(並且只執行一次)。

嘗試丟棄;從最後開始 - 我們已經修復了while循環,但現在{}循環體不會執行。這是因爲性別初始值爲0.因此,爲了讓我們的循環至少執行一次,我們需要將性別初始化爲其他值,或者使用do-while循環。

這是使用do-while的修改後的代碼。

import java.util.Scanner; 

public class AdoptAPet 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     int gender = 0; 

     double costAdoption = 0.0; 
     double costGenderSelect = 0.0; 
     double costAnyGender = 0.0; 
     double costProcessing = 0.0; 
     double total = 0.0; 
     int genderCounter = 0; 

     System.out.println("Choose from the following options: 1 for male, 2 for female, 3 for any"); 

     do //begin sentinel controlled while loop 
     { 
     System.out.print("Please choose the gender of the cat you wish to purchase:"); 
     gender = input.nextInt(); 

     if(gender < 0) 
     { 
      System.out.println("INVALID CHOICE! "); 

      System.out.print("enter 0 to quit: 0 or enter any other nunber to continue:"); 
      gender = input.nextInt(); 
     } 

     if (gender == 1) 
     { 
      costAdoption = 9.50; 
      costAdoption += costProcessing + 2.00; 
      total += costAdoption; 

      System.out.printf("Your Choice is: "); 
      System.out.printf("male and the total is $%.2f", total); 
     } 

     if (gender == 2) 
     { 
      costGenderSelect = 11.50; 
      costGenderSelect += costProcessing + 2.00; 
      total += costGenderSelect; 

      System.out.printf("Your Choice is: "); 
      System.out.printf("female and the total is $%.2f", total); 
     } 

     if (gender == 3) 
     { 
      costAnyGender = 9.50; 
      costAnyGender += costProcessing + 2.00; 
      total += costAnyGender; 

      System.out.printf("Your Choice is: "); 
      System.out.printf("Any gender and the total is $%.2f", total); 
     } 

     } while(gender != 0); //end sentinel controlled while loop 
    } 
} 
+0

非常感謝您! – jjb 2014-08-31 03:21:14

0

試試這個:

while(gender != 0);//<-- remove the semi colon,loop gets terminated by it 

也宣告性別爲:

int gender = -1;//<-- this will cause the loop to start 
+0

感謝您的幫助 – jjb 2014-08-31 03:17:28

+0

@jjb,請接受答案,如果它適合您。 – Arvind 2014-08-31 03:29:30