Java 中的数组与命令行

Array in Java with command line

我正在用命令行和数组做一个项目。一个转换 F -> C 和 C -> F 的程序。到目前为止我得到的是:

public class Implementation 
{
    public static void main(String[] args)
    {        
        double degree = 0;
        String celsius = null;
        String fahrenheit; 
        int n = 0;
        String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};       
            if (degree < 0)
            {    
            n = 0;
            }
            if (degree >= 0 && degree < 32)
            {    
            n = 1;
            }
            if (degree >= 32 && degree < 50)
            {    
            n = 2;
            }
            if (degree >= 50 && degree < 60)
            {    
            n = 3;
            }
            if (degree >= 60 && degree < 70)
            {    
            n = 4;
            }
            if (degree >= 70 && degree < 90)
            {    
            n = 5;
            }
            if (degree >= 90)
            {    
            n = 6;
            }   
        if (args.length != 3)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {

            degree = Double.parseDouble(args[0]);
            celsius = args[1]; 
            fahrenheit = args[2];        
        }

        switch (celsius)    
        {
            case "c":
            System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[n]);
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[n]);
            break;
        }
    }
    public static double celsius(double fahrenheitTemperature)
    {
        return ( 5.0 / 9.0 * ((double) fahrenheitTemperature - 32));
    }
    public static double fahrenheit(double celsiusTemperature)
    {
    return  ( 9.0 / 5.0 * (double) celsiusTemperature + 32);
    }
}

数组部分有问题。我无法获得具有特定度数的正确数组 days[]。例如,如果我执行命令行:100 c f,它应该显示 100 摄氏度是 212.0 华氏度冷度 "Hot"。但是我的程序不管我输入什么度数,都只显示"Cold"

你设置

double degree = 0;

然后

String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};    

所以当然it will result in "Cold" all the time...

这是一个完全有效的 class:

正如您在我的解决方案中看到的那样,重点是 create a function which returns the proper integer,具体取决于您提供的 degree 作为 input.. 这个 function 称为 public static int checkDegree(double degree) {。这样你就把 functionality 放在 function 中,然后 从主 class 中删除它(你的方式很成问题)this way you call the function as an argument directly, and the function gives you proper days you need for the logic you request,而不是 calling the days[n] directly,这是 导致您首先遇到的麻烦...

public class Implementation 
{
    public static void main(String[] args)
    {        
        double degree=0;
        String celsius=null;
        String fahrenheit;
        int n;
        String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"}; 
        if (args.length != 3)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {
            degree = Double.parseDouble(args[0]);
            celsius = args[1]; 
            fahrenheit = args[2];        
        }

        switch (celsius)    
        {
            case "c":    
            System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[checkDegree(degree)]);
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[checkDegree(degree)]);
            break;
        }
    }
    public static double celsius(double fahrenheitTemperature)
    {
        return ( 5.0 / 9.0 * ((double) fahrenheitTemperature - 32));
    }
    public static double fahrenheit(double celsiusTemperature)
    {
    return  ( 9.0 / 5.0 * (double) celsiusTemperature + 32);
    }
    public static int checkDegree(double degree) {
        int myReturn = 0;
        if (degree < 0)
            {    
            myReturn = 0;
            }
            if (degree >= 0 && degree < 32)
            {    
            myReturn = 1;
            }
            if (degree >= 32 && degree < 50)
            {    
            myReturn = 2;
            }
            if (degree >= 50 && degree < 60)
            {    
            myReturn = 3;
            }
            if (degree >= 60 && degree < 70)
            {    
            myReturn = 4;
            }
            if (degree >= 70 && degree < 90)
            {    
            myReturn = 5;
            }
            if (degree >= 90)
            {    
            myReturn = 6;
            }   
            return myReturn;
    }
}

输入:java Implementation 100 c f

输出:100 Celsius is 212.0 Fahrenheit Hot

你的

double degree = 0;

始终为 0。您在 if 语句后更改它,因此它始终为 0。

你可以改变这个:

switch (celsius)    
        {
            case "c":
            System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[n]);
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[n]);
            break;
        }

对此:

switch (celsius)    
        {
            case "c":
            System.out.printf("%n%s Celsius is %.5s Fahrenheit ", args[0], fahrenheit(degree));
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius ", args[0], celsius(degree));
            break;
        }

并将其移到学位声明的正上方。也移动这部分:

if (args.length != 3)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {

            degree = Double.parseDouble(args[0]);
            celsius = args[1]; 
            fahrenheit = args[2];        
        }

然后在 main() 的末尾添加:

System.out.printf("%s\n", days[n]);

这是我能想到的最快的解决方法。当然,还有大约一百万种更优雅的方法可以做你想做的事,但这超出了这个答案的范围。我强烈建议你看一些编程教程,甚至更好地阅读一本书。