-1

So the challenge is to prompt the user to enter an integer value "count". Next prompt them to enter "count" more values. Then square each value entered and add it to a main value sum.Then display the sum of the square of all the numbers entered. An example of the build output is like :

Please enter an integer value: 3 
Please enter 3 numeric values: 
7 8 3.5 
The sum of the squares of each of these numbers is: 125.25 

I'm still new to learning code, so I'm a bit lost at how to square multiple values on a single user input and also totaling them up. Can anyone offer some help?

import java.util.Scanner; 
public class Assign2 { 

    public static void main(String[] args) { 
        sum_squares(); 
    } 
    public static void sum_squares(){ 
        Scanner in = new Scanner(System.in); 
        System.out.println ("Please enter an integer value:"); 
        int count = in.nextInt(); 
        System.out.println ("Please enter" + count + "more values:"); 
        int square = in.nextInt(); 

    } 
}
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Sean Marc
  • 13
  • 3
  • 2
    can you tell which part of your problem you do not understand? can you **break down your problem**? – Kick Buttowski Oct 19 '14 at 22:46
  • When it comes to squaring the values entered at the second prompt and then totaling the results of those squared values. Im lost at how to go about doing so. For instance, in the example i posted Please enter 3 numerical values 7 8 3.5 Every example ive seen on squaring in java always uses a pre-coded int value such as i=0 int square = Math.pow(i,2) So im lost at how to go about doing this with a Scanner entry – Sean Marc Oct 19 '14 at 22:58
  • Well, I'll give you 3 hints: you need a loop (like this: `for (int i = 0; i < count; i++)`) to ask the user `count` times for a number. In your example you've entered a floating point value, therefore you can't use `in.nextInt()` to ask for the values to be squared. Use `in.nextDouble()` instead. And the last hint: use `Math.pow(number, 2)` to square `number`. – Tom Oct 19 '14 at 23:03

1 Answers1

1

You need to add a for-loop after:

System.out.println ("Please enter" + count + "more values:");

The for-loop should run for count times, and each time the loop is run, it should ask the user for an input. You can then take that input and square it (remember - squaring is as easy as multiplying a number by itself! 2 * 2 = 4, or 2-squared) Once you have the squared number, add it to a sum variable which you will have created before the for-loop. Then just print out the sum after the for-loop.

Here is a great tutorial on for-loops!

Mitch Talmadge
  • 4,638
  • 3
  • 26
  • 44