-1

所以我有这个程序,它应该接受用户输入,比如汽车名称,并在这里计算每月的价格和付款:

import java.lang.*;
import java.io.*;
import java.util.*;

public class NewCar
{

private int year;
private String make;
private String model;
private double sticker,discount,tax,finalP,final2,final3;
private int month,monPayt;
private int abbrev1;
private String abbrev2;
private String abbrev3;

   public NewCar(int year, String make, String model)
   {
      this.year= year;
      this.make= make;
      this.model = model;
   }

   public int getYear()
   {
      return year;
   }

   public String getMake()
   {
      return make;
   }

   public String getModel()
   {
      return model;
   }

   public String carDesc()
   {
      return year + " " + make + " "  + model;
   }

   public String carAbbrev()
   {
      abbrev1 = year % 100;
      String abbrev2 = make.substring(0,1);
      String abbrev3 = model.substring(0,1);
      return abbrev1 + abbrev2 + abbrev3;
   }


   public double calcFinalPrice()
   {
      final2 = (sticker - discount);
      final3 = final2 * tax;
      finalP = final2 + final3;
      return finalP;
   }

   public int calcZeroPctMonPayt()
   {
      monPayt = (int)finalP / month;
      return monPayt;
   }


   public String toString()
   {
      return "You want to purchase a " + carDesc() + ". Abbreviation: " + carAbbrev();
   }


}

我用这个测试器类测试它:

import java.lang.*;
import java.io.*;
import java.util.*;

public class NewCarTester
{





   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);

      System.out.println("Enter the car's Year, Make and Model: ");
      int year = scan.nextInt();
      String make = scan.next();
      String model = scan.next();

      System.out.println("Enter the Sticker Price: ");
      int sticker = scan.nextInt();

      System.out.println("Enter the discount: ");
      int discount = scan.nextInt();

      System.out.println("Enter the Sales Tax Rate: ");
      int tax = scan.nextInt();

      System.out.println("Enter the number of Months at Zero Percent Interest: ");
      int month = scan.nextInt();

      NewCar c = new NewCar(year, make, model);

      System.out.println(c.toString());
      System.out.println("Final Price : " + c.calcFinalPrice());
      System.out.println(c.calcZeroPctMonPayt());





    }
}

现在一切正常,接受计算方法,每当我调用其中一种计算方法时,我每次都会得到 0.0。这让我相信用户输入的输入没有正确地存储到他们应该进入的变量中。任何人都可以找出问题并帮助我解决它吗?谢谢!另外,我遇​​到的另一个问题是,如果用户输入带有小数的销售税,程序会出错并停止运行,有人对此有任何见解吗?

4

4 回答 4

1

您没有将参数作为折扣、税率等传递,就像默认情况下将它们分配为零一样。使用以下代码将解决您的问题

import java.lang.*;
import java.io.*;
import java.util.*;

public class NewCar
{

private int year;
private String make;
private String model;
private double sticker,discount,tax,finalP,final2,final3;
private int month,monPayt;
private int abbrev1;
private String abbrev2;
private String abbrev3;

   public NewCar(int year, String make, String model,double sticker,double discount,double tax,int month)
   {
      this.year= year;
      this.make= make;
      this.model = model;
      this.sticker=sticker;
      this.discount=discount;
      this.tax=tax;
      this.month=month;
   }

   public int getYear()
   {
      return year;
   }

   public String getMake()
   {
      return make;
   }

   public String getModel()
   {
      return model;
   }

   public String carDesc()
   {
      return year + " " + make + " "  + model;
   }

   public String carAbbrev()
   {
      abbrev1 = year % 100;
      String abbrev2 = make.substring(0,1);
      String abbrev3 = model.substring(0,1);
      return abbrev1 + abbrev2 + abbrev3;
   }


   public double calcFinalPrice()
   {
      final2 = (sticker - discount);
      final3 = final2 * tax;
      finalP = final2 + final3;
      return finalP;
   }

   public int calcZeroPctMonPayt()
   {
      monPayt = (int)finalP / month;
      return monPayt;
   }


   public String toString()
   {
      return "You want to purchase a " + carDesc() + ". Abbreviation: " + carAbbrev();
   }


}






 class NewCarTester
{





   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);

      System.out.println("Enter the car's Year, Make and Model: ");
      int year = scan.nextInt();
      String make = scan.next();
      String model = scan.next();

      System.out.println("Enter the Sticker Price: ");
      int sticker = scan.nextInt();

      System.out.println("Enter the discount: ");
      int discount = scan.nextInt();

      System.out.println("Enter the Sales Tax Rate: ");
      int tax = scan.nextInt();

      System.out.println("Enter the number of Months at Zero Percent Interest: ");
      int month = scan.nextInt();

      NewCar c = new NewCar(year, make, model,sticker,discount,tax,month);

      System.out.println(c.toString());
      System.out.println("Final Price : " + c.calcFinalPrice());
      System.out.println(c.calcZeroPctMonPayt());





    }
}
于 2013-10-09T04:56:48.343 回答
1

仔细看。) 方法中使用的变量, calcFinalPrice(

  final2 = (sticker- discount);

  final3 = final2 * tax;

  finalP = final2 + final3;

  return finalP;

上面使用的所有变量在创建对象时根据它们的数据类型获取默认值。

如下修改您的构造函数,您应该得到计算出的最终价格。

public NewCar(int year, String make, String model,int sticker,int discount,int tax,int month)
  {      
    this.year= year;
    this.make= make;
    this.model = model;
    this.sticker=sticker;
    this.discount=discount;
    this.tax=tax;
    this.month=month;
}
于 2013-10-09T04:46:05.223 回答
0

我在您的测试中没有看到您将税收、折扣等设置为 NewCar 对象的任何地方。

您必须为每个公共属性遵循获取和设置面向对象的方法

public double getTax()
{
   return tax;
}

public void setTax(double taxVal)
{
    this.tax = taxVal;
}

然后在您创建的对象中,您可以执行以下操作:

NewCar c = new NewCar(year, make, model);
c.setTax(5.00);

ETC..

于 2013-10-09T04:44:42.050 回答
0

sticker默认情况下,您从不实际为discounttax的实例提供NewCar0

您需要提供一些方法来将您从用户那里获得的值提供给此类

也许通过构造函数,与您设置相同的方式year,或者通过 setter 方法,例如......makemodel

public void setSticker(double sticker) {
    this.sticker = sticker;
}
于 2013-10-09T04:44:56.287 回答