Command Line and Array in Java -
i'm learning using array , command line argument java. write program convert celsius degree fahrenheit degree. started basic idea project not sure did right (i'm beginner).
public class implementation { public static void main(string[] args) { { string[] days = new string[7]; days[0] = "very cold"; days[1] = "cold"; days[2] = "mild"; days[3] = "very mild"; days[4] = "warm"; days[5] = "very warm"; days[6] = "hot"; } if (args.length != 5) { system.out.println("error! please try again."); system.exit(0); } else { string name; string convert; double degree; string celsius; string fahrenheit; name = args[0]; convert = args[1]; degree = double.parsedouble(args[2]); celsius = args[3]; fahrenheit = args[4]; system.out.printf("%n%s celsius %.2f fahrenheit\n", args[2], ( 9.0 / 5.0 * (degree + 32))); } } }
when supply command line argument, should in form:
java tempconversion 100 c f
my questions are: did command line arguments right? seem did wrong though still have same output. how show array in output specific degree? follow this:
below 0 degrees = cold
from 0 32 = cold
from 33 50 = mild
from 51 60 = mild
from 61 70 = warm
from 71 90 = warm
above 90 = hot
a couple of things:
"java" , program name (tempconversion) aren't arguments. after is. when put args.length != 5 weren't counting program name , java keyword. should use args.length != 3 (as opposed 5).
- e.g. arguments (in example) {"100", "c", "f"} assignments 0, 1, 2.
edit:
a question asked how program name , java keyword.
- the java keyword constant. can use "java" programs.
to find class name can see this post. here implementation. should work.
this won't cause grief, don't see reason brackets starting around string[] days , ending @ day[6] =...; " }". there isn't need them , string[] can have elements set , used anywhere (no braces required). else looks fine.
Comments
Post a Comment