I am making a calculator and need a subtraction loop for the code to work. Right now it is scanning the list but only doing the first and last numbers not the entire list.
if(a.equals("Subtract")) {
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.println("Enter integers one at a time in order");
System.out.println("When completed type done");
while (scan.hasNextInt()) {
list.add(scan.nextInt());
}
Integer[] nums = list.toArray(new Integer[0]);
for (int i = 0; i < nums.length-1; i++) {
sum = nums[0];
sum = sum - nums[i+1];
}
System.out.println("Your total is " + sum);
System.out.println("-----------------------------");
main(args);
}
Only subtracts the first and last numbers of loop not entire loop in order
sumeach iteration in the loop. Also, I'm not sure what the point ofsum -= sum - nums[i+1]is because you're subtracting sum from itself.sumat every iteration, which probably isn’t what you want. And the linesum -= sum - nums[i+1]doesn’t seem rational because it basically meanssum = sum + nums[i+1]