简体   繁体   English

Java中的命令行和数组

[英]Command Line and Array in Java

I'm learning using an Array and Command line argument for Java. 我正在学习使用Java的“数组和命令行”参数。 We write a program that convert from Celsius degree to Fahrenheit degree. 我们编写了一个将摄氏温度转换为华氏度的程序。 I started a basic idea for my project but not sure I did it right (I'm beginner). 我为我的项目开始了一个基本的想法,但不确定我做对了(我是初学者)。

public class Implementation 
{
    public static void main(String[] args)
    {
        {
        String[] days = new String[7];
        days[0] = "Very Cold";
        days[1] = "Cold";
        days[2] = "Mild";
        days[3] = "Very Mild";
        days[4] = "Warm";
        days[5] = "Very Warm";
        days[6] = "Hot";
        }
        if (args.length != 5)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {
            String name;
            String convert;
            double degree;
            String celsius;
            String fahrenheit;

            name = args[0];
            convert = args[1];
            degree = Double.parseDouble(args[2]);
            celsius = args[3]; 
            fahrenheit = args[4];         

            System.out.printf("%n%s Celsius is %.2f Fahrenheit\n", args[2], ( 9.0 / 5.0 *  (degree + 32)));
        }
    }
}

When I supply my command line argument, it should be in this form: 当我提供命令行参数时,它应采用以下形式:
Java TempConversion 100 cf Java TempConversion 100 cf

My questions are: Did I do my command line arguments right? 我的问题是:我是否正确执行了命令行参数? It seem like I did something wrong even though I still have the same output. 即使我仍然有相同的输出,看来我做错了什么。 How do I show up my array in the output with specific degree? 如何以特定程度在输出中显示数组? Follow this: 请遵循以下步骤:

Below 0 degrees = Very cold 低于0度=非常冷

From 0 to 32 = Cold 从0到32 =寒冷

From 33 to 50 = Mild 从33到50 =轻度

From 51 to 60 = Very mild 从51到60 =非常温和

From 61 to 70 = Warm 从61到70 =温暖

From 71 to 90 = Very warm 从71到90 =非常温暖

Above 90 = Hot 90以上=热

A couple of things: 有两件事:

  • "Java" and the program name (TempConversion) aren't arguments. “ Java”和程序名称(TempConversion)不是自变量。 Everything after that is. 之后的一切都是。 So when you put args.length != 5 you weren't counting the program name and the java keyword. 因此,当您输入args.length!= 5时,您没有在计算程序名称和java关键字。 You should use args.length != 3 (as opposed to 5). 您应该使用args.length!= 3(而不是5)。

    • eg The arguments (in the example) are {"100", "c", "f"} with the assignments 0, 1, 2. 例如,参数(在示例中)为{“ 100”,“ c”,“ f”},分配为0、1、2。

Edit: 编辑:

A question was asked as to how to get the program name and java keyword. 询问了有关如何获取程序名称和java关键字的问题。

  • The java keyword is a constant. java关键字是一个常量。 That is you can use "Java" for all programs. 也就是说,您可以对所有程序使用“ Java”。
  • To find the class name you can see this post. 要查找类名,可以查看帖子。 Here is my implementation. 这是我的实现。 Should work. 应该管用。

  • This likely won't cause you much grief, but I don't see a reason for those brackets starting around String[] days and ending at day[6] =...; 这可能不会给您带来多大的麻烦,但我看不出这些括弧从String []天开始到day [6] = ...结束的原因。 " }". “}”。 There isn't a need for them and String[] can have elements that are set and used anywhere (No braces required). 不需要它们,String []可以在任何地方设置和使用元素(不需要大括号)。 Everything else looks fine. 其他一切看起来都很好。

In java TempConversion 100 cf your command line arguments are 100 and c and f -- there are three arguments, but you are checking for five arguments in if (args.length != 5) This is unlike C where argv[0] is the program name itself. java TempConversion 100 cf您的命令行参数为100cf -有三个参数,但是您正在检查if (args.length != 5)五个参数,这与C的 argv[0]是程序名称本身。

You can see this with a minimal program 您可以通过一个最小的程序看到它

public class c2f
{
    public static void main(String[] args)
    {
        System.out.println("args.length is " + args.length);

        for (int i=0; i<args.length; i++) {
            System.out.println("arg[" + i + "] is " + args[i]);
        }
    }
}

Run this as 作为运行

javac c2f.java
java c2f 100 c f

Your output should be 您的输出应为

args.length is 3
arg[0] is 100
arg[1] is c
arg[2] is f

You are also not looking at either the "c" or the "f" arguments. 您也没有在看“ c”或“ f”参数。 Maybe you intend to add to your program so those tell you if you want to do a C->F or a F->C conversion. 也许您打算添加到程序中,以便那些程序告诉您是否要执行C-> F或F-> C转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM