循环不工作时,我的哨兵控制



我无法让while循环工作。程序提示用户是否要继续,输入0退出或任何其他整数继续。如果用户想要继续,程序将循环回菜单,否则将退出。因此,您应该有一个哨兵控制的循环,当0输入时,该循环将结束

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);
    }
    }
  }
}

此行负责:

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

仔细看。结尾有一个分号,这是整个语句。后面的{}块不属于循环(只执行一次)。

试着放下;从最后开始,我们修复了while循环,但现在{}循环体根本不执行。这是因为gender是用值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
   }
}

试试这个:

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

同时声明性别为:

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

最新更新