我现在在JAVA学习命令行。我是初学者。我为我的项目编写了一个硬代码,但我不知道如何应用命令行。
public class SalesRep
private String firstName;
private String lastName;
private String employeeID;
private double grossSales;
private double commissionRate;
public SalesRep(String[] args)
{
if (args.length != 5)
System.out.printf("Error");
else
{
firstName = args[0];
lastName = args[1];
employeeID = args[2];
grossSales = Double.parseDouble(args[3]);
commissionRate = Double.parseDouble(args[4]);
}
if (grossSales < 0.0)
throw new IllegalArgumentException
("Gross sale must be greater than or equal 0.0");
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException
("Comission rate must be in between 0.0 and 1.0");
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getEmployeeID()
{
return employeeID;
}
public void setGrossSales(double grossSales)
{
if (grossSales < 0.0)
throw new IllegalArgumentException
("Gross sales must be greater than or equal 0.0");
this.grossSales = grossSales;
}
public double getGrossSales()
{
return grossSales;
}
public void setCommissionRate(double commissionRate)
{
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException
("Comission rate must be in between 0.0 and 1.0");
this.commissionRate = commissionRate;
}
public double getCommissionRate()
{
return commissionRate;
}
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
@Override
public String toString()
{
return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f",
"Sale Representatives", firstName, lastName,
"Employee ID", employeeID,
"Gross Sales", grossSales,
"Commission Rate", commissionRate);
}
我知道命令行参数有问题。但我不知道如何解决它。有人可以帮帮我吗?
答案 0 :(得分:0)
你的程序正在进入这种情况:
if (args.length != 5)
System.out.printf("Error");
因此,&#34;错误&#34>打印在输出中。 要使用命令行参数运行程序,请使用:
java SalesRepTest arg0 arg1 arg2 arg3 arg4
其中arg0,...,arg4代表你的论点。
答案 1 :(得分:-1)
正确的代码是:
public class SalesRep{
private String firstName;
private String lastName;
private String employeeID;
private double grossSales;
private double commissionRate;
public SalesRep(String[] args)
{
if (args.length != 5)
System.out.printf("Error");
else
{
firstName = args[0];
lastName = args[1];
employeeID = args[2];
grossSales = Double.parseDouble(args[3]);
commissionRate = Double.parseDouble(args[4]);
}
if (grossSales < 0.0)
throw new IllegalArgumentException
("Gross sale must be greater than or equal 0.0");
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException
("Comission rate must be in between 0.0 and 1.0");
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getEmployeeID()
{
return employeeID;
}
public void setGrossSales(double grossSales)
{
if (grossSales < 0.0)
throw new IllegalArgumentException
("Gross sales must be greater than or equal 0.0");
this.grossSales = grossSales;
}
public double getGrossSales()
{
return grossSales;
}
public void setCommissionRate(double commissionRate)
{
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException
("Comission rate must be in between 0.0 and 1.0");
this.commissionRate = commissionRate;
}
public double getCommissionRate()
{
return commissionRate;
}
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
@Override
public String toString()
{
return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f",
"Sale Representatives", firstName, lastName,
"Employee ID", employeeID,
"Gross Sales", grossSales,
"Commission Rate", commissionRate);
}
public static void main(String [] args){
SalesRep s = new SalesRep(args);
}
}
然后你需要像
一样编译你的代码javac SalesRep.java
然后你可以运行并提供这样的命令行参数:
java SalesRep arg1 arg2 arg3 arg4 arg4
好的,这将有效。你不能为构造函数提供命令行参数,它只能用于main方法,但你可以将命令行参数作为参数传递给构造函数。