[英]How do I check if a user inputs nothing so I can display an error message
I'm trying to display an error message if the user supplies no command line arguments. 如果用户未提供任何命令行参数,我将尝试显示错误消息。 I'm getting an error when I type "java sheet12t1" into cmd. 在cmd中键入“ java sheet12t1”时出现错误。 I have the rest of my program nailed down, it's just that this part won't work. 我已经将程序的其余部分确定下来,只是这部分不起作用。
The question I'm working with is below, along with my code. 下面是我正在处理的问题以及我的代码。
This is the question I'm working with: http://i.imgur.com/Fv6901B.png 这是我正在处理的问题: http : //i.imgur.com/Fv6901B.png
public class sheet12t1
{
public static void main(String[] args)
{
int projectCategory = Integer.parseInt(args[0]);
int keyServiceType = Integer.parseInt(args[1]);
if (args.length != 2)
System.out.println("Please enter: java sheet12t1 projectCategory serviceType");
else if (args.length == 0)
System.out.println("Please enter two valid inputs.");
else if (projectCategory < 1 || projectCategory > 3)
System.out.println("Please enter valid inputs.");
else if (keyServiceType < 1 || keyServiceType > 3)
System.out.println("Please enter valid inputs.");
else
{
double result = calculateProjectCost(projectCategory, keyServiceType);
System.out.println("Total project cost: " + result);
}
}
public static double calculateProjectCost(int projectCategory, int keyServiceType)
{
double totalCost = 10000;
if (projectCategory == 1)
{
if (keyServiceType == 1) totalCost += 5600.5;
else if (keyServiceType == 2) totalCost += 5500;
else totalCost += 6000;
}
else if (projectCategory == 2)
{
if (keyServiceType == 1) totalCost += 10600.5;
else if(keyServiceType ==2) totalCost += 11000;
else totalCost += 13500;
}
else
{
if (keyServiceType == 1) totalCost += 17000;
else if(keyServiceType == 2) totalCost += 22000;
else totalCost += 25000;
}
return totalCost;
}
}
you need to check the length of the arguments array before you start using it and exit if it is wrong, change: 您需要先检查arguments数组的长度,然后才能开始使用它,如果错误则退出,请更改:
int projectCategory = Integer.parseInt(args[0]);
int keyServiceType = Integer.parseInt(args[1]);
if (args.length != 2)
System.out.println("Please enter: java sheet12t1 projectCategory serviceType");
to 至
if (args.length != 2) {
System.out.println("Please enter: java sheet12t1 projectCategory serviceType");
System.exit(1);
}
int projectCategory = Integer.parseInt(args[0]);
int keyServiceType = Integer.parseInt(args[1]);
taken from https://stackoverflow.com/a/2670980/621366 a short explanation why System.exit(0)
or System.exit(1)
摘自https://stackoverflow.com/a/2670980/621366为何使用System.exit(0)
或System.exit(1)
的简短说明
The "0" lets whomever called your program know that everything went OK. “ 0”让任何调用您的程序的人都知道一切正常。 If, however, you are quitting due to an error, you should System.exit(1);, or with another non-zero number corresponding to the specific error. 但是,如果由于错误而要退出,则应使用System.exit(1);或对应于特定错误的另一个非零数字。
Try to do something like this : 尝试做这样的事情:
public static void main(String[] args)
{
System.out.println(args.length);
}
you can place a check whether args.length is 0 or not! 您可以检查args.length是否为0!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.