我正在做一个使用命令行和数组的项目。一个转换 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 cf,它应该显示100摄氏度是212.0华氏度冷“热”。但我的程序只显示“冷”,无论我输入什么程度。
最佳答案
你设置
double degree = 0;
然后
String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};
所以当然 it will result in "Cold" all the time
...
这是一个完全的 worker 类(Class):
正如您在我的解决方案中看到的,重点是 create a function which returns the proper integer
,取决于degree
您提供为input
..这个function
称为 public static int checkDegree(double degree) {
。这样你就可以输入 functionality
在 function
并将其从主类中删除(这在您的方式中是非常有问题的)。 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
关于java - Java 中的数组与命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110653/