需要帮助在此程序中查找错误,编译但不是计算机必需的数字

时间:2015-04-26 21:57:41

标签: java

此程序编译并运行,但不计算保险费。我已经工作了好几天,无法弄明白。它之前在另一个版本中运行,但我们现在需要有2个类,现在它不计算保险,或者在每个答案中显示为零。我们还需要使用窗格进行输入。

// The Premium class represents an insurance premium cost for an individual policy.


public class Premium
{
  private int a; // to hold customer age
  private int h; // to hold customer height
  private int w; // to hold customer weight
  private String s; // to hold smoking status

  int i; // to hold insurance price

   //setAge set's customer's height 
   public void setAge(int age)
   {
      a = age;
     }

   //setHeight set's customer's height 
   public void setHeight(int height)
   {
      h = height;
     }

  //setWeight set's customer's weight
   public void setWeight(int weight)
   {
      w= weight;
    }

   //setSmoke set's customer's smoking status 
   public void setSmoke(String smoker)
   {
      s = smoker;
     }


   //getHeight method returns customer's height 
   public int getHeight()
   {
      return h;
   }

   //getWeight method returns customer's weight 
   public int getWeight()
   {
      return w;
   }

   //getAge method returns customer's age
   public int getAge()
   {
      return a;
   }

   //getSmoke returns customer's smoking status 
   public String getSmoke()
   {
      return s;
     }


   //constructor
   public Premium(int age, int height, int weight, String smoker)
   {
    age = a;
    height = h;
    weight = w;
    smoker = s;
   }


   //calculate individual insurance price method 
      public static int calculateInsurance(int age, String smoker) 
      {
         //variables
         String smoking = "smoker";
         int insurance;
         final int insuranceBase = 400;

         //create new instance of BMI class
          BMI custBMI = new BMI();

          double bmi= custBMI.calculateBMI();


         //calculate insurance
         if (age > 50){
            insurance = insuranceBase + 100;
            }
            else{
             insurance = insuranceBase;
            }

          if (smoker.equals('Y')){
            insurance += 150;
            }else {
            insurance +=0;
            }

           if (bmi > 40){
            insurance += 100;
            } else {
            insurance +=0;
            }

       return insurance;

       }

   //setInsurance method sets insurance price
    public void setInsurance(int insurance)
    {
      i = insurance;
     }

   //getInsurance method returns insurance price
   public int getInsurance()
   {
      return i;
   }



  public void displayPrice()
  {
   System.out.println("Insurance Premium Cost: " + getInsurance());
  }
}

*************************************
/** BMI class calculates customer's BMI
*/

public class BMI
{
   private int h; //to hold customer's height
   private int w; //to hold customer's weight

   //setWeight set's customer's weight
   public void setWeight(int weight)
   {
      w= weight;
    }
   //set height set's customer's height 
   public void setHeight(int height)
   {
      h = height;
     }

   //getWeight method returns customer's weight 
   public int getWeight()
   {
      return w;
   }

   //getHeight method returns customer's height 
   public int getHeight()
   {
      return h;
   }

   // calculate customer's BMI  
   public double calculateBMI()
      {  
         double bmi; 
         //calculate BMI 
         bmi = (getWeight() * 703)/(getHeight() * getHeight());

         return bmi;
      }


 }
*******************************************

  This program will demonstrate the Premium and BMI classes. This program will prompt users for input, 
  use that input to calculate BMI and their insurance rate.
  Output will be the customer profile: name, age, smoker status, height, weight and insurance premium cost. 
  Program will then ask user if they want to create another profile. 
*/

import javax.swing.JOptionPane; //required for dialog boxes

public class Project2_1
{
    public static void main(String[] args)
    { 
      String submit;

      System.out.println("Welcome! Please fill out the following information to get an insurance premium quote.");
      System.out.println();


     do{
     //get customerInfo method promts user for their information
      String input; // to hold user input
      String fname;
      String lname;
      int age;
      String smoker;
      int height;
      int weight;


      //get customer input
      fname =JOptionPane.showInputDialog("Please enter your first name: ");
      lname =JOptionPane.showInputDialog("Please enter your last name: ");
      input = JOptionPane.showInputDialog("Please enter your age: ");
         age = Integer.parseInt(input);
      //validate input
      while (age < 0 || age > 141)
      { 
         input = JOptionPane.showInputDialog("Invalid input! Age must be 1 through 140. Please enter your age: ");
         age = Integer.parseInt(input);
      }

      smoker = JOptionPane.showInputDialog("Are you a smoker? Please enter Y or N: ");
      //validate input
      if (!smoker.equalsIgnoreCase("Y"))  
      { 
        System.out.println("");   
         }else if (!smoker.equalsIgnoreCase("N"))
         {
         smoker = JOptionPane.showInputDialog("Invalid input! Are you a smoker? Answer must be Y or N: ");
         } else 
            {
               System.out.println("");
               }
      input = JOptionPane.showInputDialog("Please enter your height in inches: ");
         height = Integer.parseInt(input);
      //validate input
      while (height < 0 || height > 108)
       { 
         input = JOptionPane.showInputDialog("Invalid input! Height must be between 1 and 108 inches. Please enter your height in inches: ");
         height = Integer.parseInt(input);
      }
      input = JOptionPane.showInputDialog("Please enter your weight in pounds: ");
         weight= Integer.parseInt(input);
      //validate input
      while (weight < 0 || weight > 800)
       { 
         input = JOptionPane.showInputDialog("Invalid input! Weight must be between 1 and 800 pounds. Please enter your weight in pounds: ");
         weight = Integer.parseInt(input);
      }

    //create instance of the Premium class

     Premium customer = new Premium(age, height, weight, smoker);


     //call method to display individual results                 
     displayResults(fname, lname, age, smoker, height, weight);    

     //get data from customer and displays  calculated results
      customer.displayPrice();

     submit =JOptionPane.showInputDialog("Would you like to submit another profile? Enter Y or N: ");
     //validate input
       if (!submit.equalsIgnoreCase("Y"))  
      { 
        System.out.println("");  

         } else if (!smoker.equalsIgnoreCase("N"))

            {  submit = JOptionPane.showInputDialog("Invalid input!Another profile? Answer must be Y or N: ");

            } else {
               System.out.println("");
             }  
      } while (submit.equalsIgnoreCase("Y"));

  }  

      //method to display customer profile information
     public static void displayResults(String fname, String lname, int age, String smoker, int height, int weight)
      {

           //Output  
           System.out.println(" ");
           System.out.println("First Name:             " + fname); 
           System.out.println("Last Name:              " + lname);  
           System.out.println("Age:                    " + age);
           System.out.println("Smoking Status:         " + smoker);
           System.out.println("Height(in.):            " + height);
           System.out.println("Weight(lbs):            " + weight);
           System.out.println(" ");
   }

 }

1 个答案:

答案 0 :(得分:0)

如果您没有收到任何编译器错误,那么您的代码中就会出现逻辑错误。

在我看来,永远不会调用calculateInsurance方法。