Results 1 to 2 of 2

# Thread: Problem ...

- 10-09-2011, 06:23 PM #1Member
- Join Date
- Oct 2011
- Posts
- 16

- Rep Power
- 0

## Problem ...

Ei guys can you help me ..

Java Code:import java.io.*; public class Sum100Numbers { public static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); public static void main(String args [])throws Exception { int total = 0; // stores the sum of the 5 numbers int number; // stores the current input String again; int cust = 0; int total2 = 0; int total3= 0; do { System.out.print( "Enter an integer > " ); number = Integer.parseInt(input.readLine()); System.out.print( "Dou you want to enter again ? " ); again = input.readLine(); cust++; total3 = number; total2++; }while(again.equalsIgnoreCase("y")); if(again.equalsIgnoreCase("n")) { for ( int i = 1;i<=cust ; i++ ) { total += number; System.out.print("Customer "+i+": "); if(total2 >= i){ total3 = number; System.out.print("\t"+" "+total3); } System.out.println(); } System.out.println( "The total :"+"\t"+" " + total ); } } }

example:

if i enter integer : 5

it will loop ask you a question to enter again? , if i enter Y it will Loop again and ask the user to enter an integer

again and if he/she want to enter again .. if i enter integer 6 .. then answer N this will become the output ..

Customer 1:..5

Customer 2:..6

===========

Total..........:.11

- 10-11-2011, 11:30 PM #2Member
- Join Date
- Oct 2011
- Posts
- 65

- Rep Power
- 0

## Re: Problem ...

You have to use array to allocate numbers that are taken from user.

In your code:

total3 = number; // you stores integer value into same location in memory for every read input.

EX:

cust=0, total2=0 , total3=0, again="";

First LOOP:

integer is entered : 5;

user wants to continue: again="y";

cust=1; total2=1; total3=5;

END FIRST LOOP

Second LOOP;

integer is entered :7;

user does not want to continue: again="n or anything except y";

cust=2;total2=2;total3=7;

END SECOND LOOP;

AFTER LOOP :> cust=2;total2=2;total3=7;

customers entered 2 integer. Number of customer: cust=2 //TRUE

Entered numbers: total3=7 // but your code lost previous value. <!-- MOST IMPORTANT PART -->

In order to keep integer values from each customer , you can insert integer values into array.

array[0]=5; //System.out.println("First given integer: "+array[0]);

array[1]=7; //System.out.println("Second given integer: "+array[1]);

int [] array=new int[1000]; // you have 1000 integer memory allocation that are empty. I guess they are initially zero.

//Print array values and sum of values.

int total=0;

for(int i=0;i<cust;i++){

System.out.println((i+1)+". Customer :"+ array[i]);

total+=array[i];

}

System.out.println("Total: "+ total);

I explained this because of "answer N this will become the output " sentences

## Bookmarks