Java中带命令行的数组
我正在用命令行和数组做一个项目。一个转换F->C和C->F的程序。这里是我到目前为止得到的:Java中带命令行的数组,java,arrays,command-line,Java,Arrays,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; St
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);
}
}
公共类实现
{
公共静态void main(字符串[]args)
{
双度=0;
字符串=空;
华氏线;
int n=0;
字符串[]天={“非常冷”、“冷”、“温和”、“非常温和”、“温暖”、“非常温暖”、“热”};
如果(度<0)
{
n=0;
}
如果(度>=0&&度<32)
{
n=1;
}
如果(度>=32和度<50)
{
n=2;
}
如果(度>=50和度<60)
{
n=3;
}
如果(度>=60和度<70)
{
n=4;
}
如果(度>=70和度<90)
{
n=5;
}
如果(度>=90)
{
n=6;
}
如果(参数长度!=3)
{
System.out.println(“错误!请重试。”);
系统出口(0);
}
其他的
{
degree=Double.parseDouble(args[0]);
摄氏度=args[1];
华氏温度=args[2];
}
开关(摄氏度)
{
案例“c”:
System.out.printf(“%n%s摄氏度为%.5s华氏度%s\n”,参数[0],华氏度,天数[n]);
打破
案例“f”:
System.out.printf(“%n%s华氏度为%.5s摄氏度%s\n”,参数[0],摄氏度,天[n]);
打破
}
}
公共静态双摄氏度(双华氏温度)
{
返回(5.0/9.0*((双)华氏温度-32));
}
公共静态双华氏度(双摄氏度)
{
返回(9.0/5.0*(双)摄氏度+32);
}
}
我在数组部分有问题。我无法获得具有特定度数的正确数组天[]。例如,如果我使用命令行:100cf,它假定显示100摄氏度是212.0华氏度冷“热”。但是我的程序只显示“冷”,不管我输入的是什么程度 您设置了
然后
所以当然,它会一直导致“冷”
这是一个完全的工人阶级:
正如您在我的解决方案中所看到的,整个要点是创建一个返回正确整数的函数,具体取决于作为输入提供的度。。这个函数
被称为公共静态int-checkDegree(double degree){
。通过这种方式,您将功能
放在函数中
并将其从主类中删除(这样做很有问题).通过这种方式,您可以直接将函数作为参数调用,该函数为您请求的逻辑提供所需的适当天数
,而不是直接调用天数[n]
,这会导致您首先遇到的问题
公共类实现
{
公共静态void main(字符串[]args)
{
双度=0;
字符串=空;
华氏线;
int n;
字符串[]天={“非常冷”、“冷”、“温和”、“非常温和”、“温暖”、“非常温暖”、“热”};
如果(参数长度!=3)
{
System.out.println(“错误!请重试。”);
系统出口(0);
}
其他的
{
degree=Double.parseDouble(args[0]);
摄氏度=args[1];
华氏温度=args[2];
}
开关(摄氏度)
{
案例“c”:
System.out.printf(“%n%s摄氏度为%.5s华氏度%s\n”),args[0],华氏度(度),天[checkDegree(度)];
打破
案例“f”:
System.out.printf(“%n%s华氏度为%.5s摄氏度%s\n”),args[0]、摄氏度(度)、天[checkDegree(度)];
打破
}
}
公共静态双摄氏度(双华氏温度)
{
返回(5.0/9.0*((双)华氏温度-32));
}
公共静态双华氏度(双摄氏度)
{
返回(9.0/5.0*(双)摄氏度+32);
}
公共静态整数校验度(双度){
int myReturn=0;
如果(度<0)
{
myReturn=0;
}
如果(度>=0&&度<32)
{
myReturn=1;
}
如果(度>=32和度<50)
{
myReturn=2;
}
如果(度>=50和度<60)
{
myReturn=3;
}
如果(度>=60和度<70)
{
myReturn=4;
}
如果(度>=70和度<90)
{
myReturn=5;
}
如果(度>=90)
{
myReturn=6;
}
返回我的返回;
}
}
输入:java实现100cf
输出:100摄氏度是212.0华氏度热
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]);
这是我能想象到的最快的修复方法。当然,还有大约一百万种更优雅的方法来做你想做的事情,但它超出了这个答案的范围。我强烈建议你看一些编程教程,或者最好读一本书。看起来你在得到参数之前设置了n,所以
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];
}
System.out.printf("%s\n", days[n]);