2014-10-19 114 views
-1

所以挑戰在於提示用戶輸入一個整數值「count」。接下來 提示他們輸入「count」更多的值。然後將輸入的每個值平方並將其添加到主值 值總和中。然後顯示輸入的所有數字的平方和。 的生成輸出的例子是這樣的:在java中進行平方和總計

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 

我還是新學習的代碼,所以我在如何在單個用戶輸入方多個值有點失落,也合計起來。任何人都可以提供幫助嗎?

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(); 

    } 
} 
+2

你能告訴你哪部分問題你不明白嗎?你可以**解決你的問題**? – 2014-10-19 22:46:44

+0

說到在第二個提示中輸入的值的平方,然後總計這些平方值的結果。我失去了如何去做這件事。例如,在我發佈的示例中, 請輸入3個數值 7 8 3.5 每個在java中顯示的平均值的示例總是使用預編碼的int值,如 i = 0 int square = Math.pow(i ,2) 所以我失去了如何去做這個掃描器條目 – 2014-10-19 22:58:47

+0

好吧,我會給你3個提示:你需要一個循環(像這樣:for(int i = 0; i Tom 2014-10-19 23:03:44

回答

1

你還要添加一個for循環:

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

的for循環應該count時間運行,並且每個迴路運行時,它應該詢問用戶輸入。然後,您可以接受該輸入並將其平方(記住 - 平方就像自己乘以一個數字一樣簡單!2 * 2 = 4或2平方)一旦有了平方數,將它添加到總和變量中,您將在for循環之前創建。然後在for循環之後打印出總和。

Here is a great tutorial on for-loops!

+0

酷!很高興你能得到它的工作!祝你有美好的一天! :) – 2014-10-19 23:16:47

+0

'+ 1'總有辦法說謝謝。 @MitchTalmadge – afzalex 2014-10-20 00:44:20

+0

@afzalex哇,謝謝!你真棒 :) – 2014-10-20 00:45:55