简体   繁体   English

Java中带命令行的数组

[英]Array in Java with command line

I'm doing a project with command line and array. 我正在做一个带有命令行和数组的项目。 A program that converts F -> C and C -> F. Here what I got so far: 一个将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);
    }
}

I have problem in Array part. 我在阵列部分有问题。 I couldn't get the right array days[] with the specific degree. 我无法获得具有特定度数的正确数组天[] For example, If I do command line: 100 cf , it supposes to show up 100 Celsius is 212.0 Fahrenheit Cold "Hot" . 例如,如果我执行命令行: 100 cf ,则假设显示100摄氏度是212.0华氏度“热” But my program only shows up "Cold" , no matter what degree I input. 但是,无论我输入什么程度,我的程序只会显示“冷”

you set 你设置

double degree = 0;

then 然后

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

so of course it will result in "Cold" all the time ... 所以当然, it will result in "Cold" all the time ...

Here is a fully working class: 这是一个完全工作的类:

As you can see in my solution the whole point is to create a function which returns the proper integer , depending on the degree you provide as input .. this function is called public static int checkDegree(double degree) { . 如您在我的解决方案中所看到的,重点是create a function which returns the proper integer根据您提供的input degree create a function which returns the proper integer 。此function称为public static int checkDegree(double degree) { This way you put the functionality in a function and remove it from the main class (which is very problematic the way you had it) . 这样,你把functionalityfunction ,并从主类中删除(这是非常有问题的,你有它的方式)。 this way you call the function as an argument directly, and the function gives you proper days you need for the logic you request , instead of calling the days[n] directly , which was causing the troubles you were faced with in the first place... 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;
    }
}

INPUT : java Implementation 100 cf 输入: java Implementation 100 cf

OUTPUT : 100 Celsius is 212.0 Fahrenheit Hot 输出: 100 Celsius is 212.0 Fahrenheit Hot

Your 你的

double degree = 0;

remains always 0. You change it after your if statements so it is always 0. 始终保持为0。在if语句后更改它,因此始终为0。

You can change this: 您可以更改此:

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;
        }

to this: 对此:

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;
        }

and move it just right above your degrees declaration. 并将其移动到学位声明上方。 Move this part too: 也移动此部分:

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];        
        }

Then at the end of your main() add this: 然后在main()的末尾添加以下内容:

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

That's the quickest fix I can imagine. 这是我能想象的最快的修复方法。 Of course there are about a million more elegant ways to do what you want but it goes out of this answer's scope. 当然,有大约一百万种更优雅的方法可以执行您想要的操作,但这超出了此答案的范围。 I strongly recommend you to watch some programming tutorials or even better read a book. 我强烈建议您观看一些编程教程,甚至最好读一本书。

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

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