java - Math operations conditional statement -
public class eval { public static void main(string[] args) { int operand1; char exp1; int operand2; if (args.length != 3 ) { system.err.println("*** program needs 3 arguements***"); system.err.println("usage: java eval int1 exp int2"); system.exit(1); } operand1 = integer.parseint(args[0]); exp1 = args[1].charat(0); operand2 = integer.parseint(args[2]); system.out.print(args[0] + args[1] + args[2] + "="); switch(exp1){ case('-'): system.out.println(operand1 - operand2); break; case('+'): system.out.println(operand1 + operand2); break; case('/'): system.out.println(operand1 / operand2); break; case('*'): system.out.println(operand1 * operand2); break; default: system.out.println(" error.invalid operator."); } } } this program mathematical operations. program can multiply integers, why happen? furthermore, why (usuage: java eval) part necessary -- doesn't work without it. there way avoid doing this? lastly why args necessary. i'm sorry many questions, don't want blindly write program no clear understanding of i'm doing. thank , once again sorry many questions!
the problem multiplication not in java, command line itself. * character special on command lines, escape in command line , should fine:
$ java eval 6 * 9 *** program needs 3 arguements*** usage: java eval int1 exp int2 $ java eval 6 \* 9 6*9=54 the args necessary here because java takes command line arguments , passes them program through main parameter args.
you must java eval because you're running program java -- jvm itself. first argument class containing main method run. other command line arguments passed args parameter of main.
the java tutorial on subject explains how command-line arguments work.
Comments
Post a Comment