在JAVA中使用此Scanner类的用户输入

时间:2022-10-24 21:54:45

So I have this program that is supposed to take user input like car names and calculate prices and payment per month right here:

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

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


}

And i test it with this tester class:

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

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





    }
}

Now everything works fine accept the calcualtion methods, Whenever i call one of the calculation methods i get 0.0 everytime. This leads me to believe that the inputs the user is putting in are not correctly getting stored into the variables which they are supposed to go into. Can anyone identify the problem and help me fix it? Thanks! Also, one more problem im having is that if a user enters the sales tax with a decimal in it, the program gets an error and stops running, anyone have any insight on that?

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

4 个解决方案

#1


1  

You are not passing the arguments as discount , tax rate etc pass them like following by default the are being assigned zero. Use the following code it will solve your problem

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

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





    }
}

#2


1  

Look closely. the variabkle used in calcFinalPrice() method i.e.

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

  final2 = (sticker- discount);

  final3 = final2 * tax;

  finalP = final2 + final3;

  return finalP;

All the variable used above get default value at the time of object creation based on their data type.

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

Modify your constructor as below and you should get the calculated final price.

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

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

#3


0  

You never actually supply values for sticker, discount or tax to the instance of NewCar, making 0 by default.

您实际上从未向NewCar实例提供贴纸,折扣或税的值,默认情况下为0。

You need to supply some way to supply the values you get from the user to this class

您需要提供一些方法来提供从用户到此类的值

Perhaps via the constructor, the same way you've set year, make and model or via setter methods, for example...

也许通过构造函数,就像你设置年份,制作和模型或通过setter方法一样,例如......

public void setSticker(double sticker) {
    this.sticker = sticker;
}

#4


0  

I dont see anywhere in your test where you are setting the Tax, discount etc... to the NewCar object.

我没有在你的测试中看到你在哪里设置税,折扣等......到NewCar对象。

You must follow the get and set object oriented approach for each of your public properties

您必须为每个公共属性遵循get和set面向对象的方法

public double getTax()
{
   return tax;
}

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

Then in your created object you can do something like:

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

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

etc..

#1


1  

You are not passing the arguments as discount , tax rate etc pass them like following by default the are being assigned zero. Use the following code it will solve your problem

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

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





    }
}

#2


1  

Look closely. the variabkle used in calcFinalPrice() method i.e.

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

  final2 = (sticker- discount);

  final3 = final2 * tax;

  finalP = final2 + final3;

  return finalP;

All the variable used above get default value at the time of object creation based on their data type.

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

Modify your constructor as below and you should get the calculated final price.

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

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

#3


0  

You never actually supply values for sticker, discount or tax to the instance of NewCar, making 0 by default.

您实际上从未向NewCar实例提供贴纸,折扣或税的值,默认情况下为0。

You need to supply some way to supply the values you get from the user to this class

您需要提供一些方法来提供从用户到此类的值

Perhaps via the constructor, the same way you've set year, make and model or via setter methods, for example...

也许通过构造函数,就像你设置年份,制作和模型或通过setter方法一样,例如......

public void setSticker(double sticker) {
    this.sticker = sticker;
}

#4


0  

I dont see anywhere in your test where you are setting the Tax, discount etc... to the NewCar object.

我没有在你的测试中看到你在哪里设置税,折扣等......到NewCar对象。

You must follow the get and set object oriented approach for each of your public properties

您必须为每个公共属性遵循get和set面向对象的方法

public double getTax()
{
   return tax;
}

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

Then in your created object you can do something like:

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

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

etc..