2014-08-30 95 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