简体   繁体   English

如何检查用户是否未输入任何内容,以便显示错误消息

[英]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.

相关问题 如何让我的输入验证工作,以便 -1 - 100 范围之外的任何内容都将显示错误消息? - How can I get my input validation to work so anything outside range -1 - 100 will display the error message? 我如何在 Java 中显示错误消息 - How can i display an error message in Java 我希望我的程序在用户输入除整数以外的任何内容并将其循环回时显示错误消息 - I want my program to display an error message when the user inputs anything other than an integer and loop it back 如果用户输入错误的数字或如果用户输入“1”,我该如何让它重新开始 - How do I make it so that it stars over if the user inputs wrong number OR start over if user types in “1” 当用户在中间输入 2 个带有“和”的值时,我如何做到这一点,它会执行一种情况? - How do I make it so when the user inputs 2 values with 'and' in the middle it executes one case? 如果用户输入了无效字符,我如何使我的程序向用户输出消息? - If a user inputs an invalid character how can I get my program to output a message to the user? 如何创建用户输入列表? - How do I create a list of user inputs? 如何使用户可以选择一个人的性别? - How do I make it so that the user can choose the gender of a person? 如果找不到文件,如何显示错误消息? - How do I display an error message if a file is not found? 如何在循环中捕获重复出现的用户输入,然后在程序结束时全部显示它们? - How can I capture recurring user inputs in a loop and then display them all at the end of the program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM