CommandLinejava Please help me the JAVA program Write a prog
CommandLine.java Please help me the JAVA program
Write a program that takes a series of int values as command line arguments. Calculate a total from the values, but add the values that are even, and subtract the values that are odd. Note that you\'re basing the add/subtract operation on the value, not on the position in the argument list. For example, if you call your class CmdLineProc, java CmdLineProc 1 2 4 3 5 7 6 8 4 3 should calculate -1 +2 +4 -3 -5 -7 +6 +8 +4 -3Solution
CmdLineProc.java
public class CmdLineProc {
public static void main(String[] args) {
int total = 0;
for(int i=0; i<args.length; i++){
int n = Integer.parseInt(args[i]);
if(n % 2 == 0){
total = total + n;
System.out.print(\" + \"+n);
}
else{
total = total - n;
System.out.print(\" - \"+n);
}
}
System.out.println(\" = \"+total);
}
}
Output:
- 1 + 2 + 4 - 3 - 5 - 7 + 6 + 8 + 4 - 3 = 5
