Java初学者中的数组

时间:2015-03-16 23:28:28

标签: java arrays string command-line

我是编码的初学者。我们编写了一个项目,通过使用命令行参数将度数从F转换为C,将C转换为F.这是我到目前为止所得到的:

public class Implementation 
{
    public static void main(String[] args)
    {
        {
        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
        {
            double degree;
            String celsius;
            String fahrenheit;

            degree = Double.parseDouble(args[0]);
            celsius = args[1]; 
            fahrenheit = args[2];                 
        switch (celsius)    
        {
            case "c":
            System.out.printf("%n%s Celsius is %s Fahrenheit\n", args[0], ( 5.0 / 9.0 * (degree - 32)));
                break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %s Celsius\n", args[0], ( 9.0 / 5.0 * (degree + 32)));
                break;
        }
        }
    }
}

我们有学位范围:

  

低于0度=非常冷

     

从0到32 =感冒

     

从33到50 =温和

     

从51到60 =非常温和

     

从61到70 =温暖

     

从71到90 =非常温暖

     

高于90 =热

我对数组有疑问。我们如何在输出中显示数组取决于具体的程度?拜托,非常感谢!

3 个答案:

答案 0 :(得分:1)

您在自己的范围内定义了几天,这就是为什么它无法访问:

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

只需移除周围的花括号即可。

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

答案 1 :(得分:0)

if (args.length != 3)

之前似乎还有一个额外的花括号
} //This is extra
        if (args.length != 3)

答案 2 :(得分:0)

如上所述,您应该只创建嵌套的if语句。我发现在一个可以从两种情况调用的单独方法中更容易实现。我很难,你不会应用相同的#34;热度分类"对于Celcius和Fahrenheit学位,嘿!

public class Implementation 
    {
        public static void main(String[] args)
        {

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

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

                switch (celsius)    
                {
                case "c":
                  System.out.printf("%n%s Celsius is %s Fahrenheit\n", args[0], ( 5.0 / 9.0 * (degree - 32)));
                  //As the method return string (from the string array 'days') the string can just be printed out.
                  System.out.println( howCold( 5.0 / 9.0 * (degree - 32) ));
                case "f":
                  System.out.printf("%n%s Fahrenheit is %s Celsius\n", args[0], ( 9.0 / 5.0 * (degree + 32)));
                  System.out.println( howCold( 9.0 / 5.0 * (degree + 32) ));
                }

            }
        }

        //Separate method
        //The method takes the converted temperature (d) as a parameter.
        public static String howCold(double d){

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

          if(d < 0){
            return days[0];
          }
          if(d >= 0 && d <= 32){
            return days[1];
          }

          //And so on...

          }
          else{
            return days[6];
          }
        }
    }

没有必要使用数组难度,因为你可以只返回一个字符串:

if(d < 0){
   return "Very cold";
}