2017-11-08 4 views
-1

私は、0からユーザーが入力した数値までの偶数の合計を出力するプログラムを作成しています。たとえば、ユーザーが20番を入力した場合、プログラムは0と20の間のすべての偶数の合計を計算します。偶数の整数のJavaプログラムの無限ループ

数字10のプログラムをテストしたところ、しかし、私は別の番号、35を使用してみました、そして、それはちょうど無限ループで立ち往生していました。私は何かすべての助けに感謝します。コードは以下に掲載されます:

(編集)フィードバックありがとうございました!友人と話をした後、私たちは解決策が実際にはとても簡単だと気付きました。私たちはただそれを複雑にしていました。それでも、すべての提案に感謝します。

//************************************************************** 
// Prints the sum of the even numbers within a range of 0 
// and the integer that the user enters. 
// 
// @me 
// @version_1.0_11.7.17 
//************************************************************** 
import java.util.Scanner; 

public class EvenNumbersSum 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     int user_num = 2; // variable that stores the user's number 
     int sum; // stores the sum of the needed values 

     System.out.print("Enter an integer greater than or equal to, 2: "); // prompt user for input 
     user_num = input.nextInt(); 

     // checks to see if the value entered is valid or not. 
     while (user_num < 2) 
     { 
      System.out.println("Invalid entry. Must enter an integer greater than or equal to, 2.\n"); 
      System.out.print("Enter an integer greater than or equal to, 2: "); 
      user_num = input.nextInt(); 
     } 

     // starts adding the values 
     for (sum = 0; sum <= user_num;) 
     { 
      if (user_num % 2 == 0) // checks if the number is even 
       sum+=user_num; // add the number to sum 
      else 
       continue; // I thought that I might need this, but ended up changing nothing. 
     } 

     System.out.println(); // extra line for cleanliness 
     System.out.printf("The sum of the even numbers between 0 and %d is %d.", user_num, sum); // prints the result 
    } 
} 
+0

user_numはforループ内で変更されません。もう一度入力を受け入れる必要があります。 –

+0

forループ内の 'sum'と' user_num'を2項で比較しているのはなぜですか? – John3136

+0

'for(sum = 0; sum <= user_num;)'は意味をなさないので、 'user_num'回ループしませんか?その数値を合計するかどうかを決定するためにその値を使用します。 – MadProgrammer

答えて

1

なぜあなたはこのためのループを書いていますか、それを行う効率的な方法があります。あなたは

NOTE偶数を追加したいと思いますどのまで

Sum of numbers between 1-N = (N(N+1))/2 

Sum of even numbers between 1-N = (N(N+2))/4 

N =ユーザー与えられた入力数:あなたはそれもによって(N%2 == 0)だと入力番号の検証を追加することができますそして、それは

0

あなたが条件で使用した変数ではない場合はエラーを返す(つまり合計 & user_num)誰が奇数の場合には変化しないし、あなたのコードがループを決して終わることのないで立ち往生。 カウンタ変数を使用してください(例:i〜1からuser_numまで)。その番号を条件に使用してください。例:

// starts adding the values 
    sum = 0; 
    for (int i = 0; i <= user_num; i++) 
    { 
     if (i % 2 == 0) // checks if the number is even 
      sum+=i; // add the number to sum 
    } 
0

forループは次のようになります。

int total_sum = 0; 
for (int sum = 0; sum <= user_num; sum++) 
     { 
      if (sum % 2 == 0) // checks if the number is even 
       total_sum+=sum; // add the number to total sum 
      else 
       continue; // I thought that I might need this, but ended up changing nothing. 
     } 
// note print total sum 
System.out.println(totalsum); 

最初に入力した番号をチェックしているプログラムは、偶数または奇数です。 そして入力された数値を合計します。 合計が常に入力された数値の2倍だったので、偶数です。 入力された数字が奇数の場合は、入力された数字(奇数)%2 == 0が常に偽であり、else文が実行されるため無限ループになります。

+0

ありがとうございました!私はちょうど私のコードが数分前に何をしているのかを理解しました。私はちょうどその時に巻き込まれていて、思っていなかったと思う。私はそれを修正し、更新を与えるしようとします! – Alphox

関連する問題