Looping to Calculate a total of String array? Java -
my coding below beginner @ stage , need help, lecturer asked me " use loop calculate total of annual rents in array" means user enters rent on 12 months, how make loop calculate sum of numbers not entered in code , going through array, put code array in here , appreciated.
scanner keyboardscanner = new scanner(system.in); double[] array = new double[12]; (int = 0; < 12; i++) { system.out.println("enter rental month[" + + "]"); array[i] = keyboardscanner.nextdouble(); } (int = 0; < array.length; i++) { system.out.println(array[i]); }
let's take @ current code:
scanner keyboardscanner = new scanner(system.in);
this loads in scanner-object, in case being used read input given user.
double[] array = new double[12];
you want save numeric values every month in year. year has 12 months, hence array of doubles created. list of numerics (with size 12), can save value each month in here.
for (int = 0; < 12; i++) {
so, before can anything, need know input of user. user needs insert value 12 times. hence, ask user 12 times insert value, save value in our created array.
the perfect way achieve using for-loop. essentially, loop does, following: first, create counter tick through loop, , name i
. set 0. next, tell loop when stop looping. in case, stop if i
same or higher 12. next, tell loop after each loop. in case, increase value 1 using i++
.
system.out.println("enter rental month[" + + "]"); array[i] = keyboardscanner.nextdouble(); }
the value user inserts, needs saved. each time user inserts value, can collect value using keyboardscanner.nextdouble()
. array you've created has 12 places: array[0] array[11]. since let i
start on 0 , end on 11, can set value in array on value inserted. so, value inserted 3th time (i
=2) gets saved on array[2]
.
for (int = 0; < array.length; i++) { system.out.println(array[i]); }
now, going same again. complete length of array
, outputting value in array. result in: system.out.println(array[0]); system.out.println(array[1]); system.out.println(array[2]); ...
now, think of can array
. you're printing every number in system.out.println(array[i]);
. so, value array
contains numbers.
you should create new double
variable in can store total value. , then, add values array double
.
so, going edit original code:
scanner keyboardscanner = new scanner(system.in); double[] array = new double[12]; (int = 0; < 12; i++) { system.out.println("enter rental month[" + + "]"); array[i] = keyboardscanner.nextdouble(); } //declaring variable totalsum, holds total sum of values in array. double totalsum = 0; //so now, need array , sum values in array. (int = 0; < array.length; i++) { system.out.println(array[i]); } //outputting result of calculation system.out.println("the total value is: " + totalsum);
allright, let's put in textual way:
- i declare variable named totalsum. set value 0.
- for each value in array, meaning each month, add amount totalscore totalsum.
- and output totalsum, user sees sum of months.
so, final touch, change following:
//so now, need take each value , add them totalsum (int = 0; < array.length; i++) { system.out.println(array[i]); totalsum = totalsum + array[i]; }
Comments
Post a Comment