Java calculate the sum of the given numbers

This program calculates the sum from 10 numbers, you could hit “stop” to break and calculate less than 10 numbers:

public class HelloWorld{
     public static void main(String []args){
        int[] values = new int[10];
        int i = 0;
        int sum = 0;
        System.out.print("write up to 10 numbers !\n");//Info text
        for(i = 0; i <= values.length-1;++i){
           System.out.print("write your number: ");//Info text
           String str = System.console().readLine();
           if(!str.equals("stop")){
           values[i] = Integer.parseInt(str);//read input
           }else{--i;break;}
        }
        System.out.print("your input:\n");//Info text
        for(int x = 0; x <= i;++x){
           System.out.print("your number: "+values[x]+"\n");//Info text
           sum += values[x];
        }
        System.out.println("your result: "+sum);//print result
     }
}

Output looks like this:

write up to 10 numbers !
write your number: 1 
write your number: 2 
write your number: 3 
write your number: stop 
your input:        
your number: 1 
your number: 2 
your number: 3 
your result: 6

3 thoughts on “Java calculate the sum of the given numbers”

  1. I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get got an impatience over
    that you wish be delivering the following. unwell unquestionably come further formerly again since exactly
    the same nearly very often inside case you shield this hike.

  2. In fact no matter if someone doesn’t understand after that its up to other viewers that they will
    assist, so here it occurs.

Leave a Reply

Your email address will not be published.

18 − 8 =