循环 - 两个包含整数之间偶数的和

时间:2017-03-05 20:56:34

标签: java loops while-loop sum integer

编辑:大声笑我从来没想过你们会是这样的野人。我认为这是一个大学生来寻求答案的合适场所。如果我不在乎,为什么我会在这里...基本上我的问题与其他问题不同,因为我确实需要第一个循环来说明我的偶数,即:10 12 14 16 18 20.我真诚地希望我会得到一些关于如何在第二个循环中恢复firstNum的原始值的反馈,而不是批评期望在几秒钟内被选为点数的答案。

我已经实现了一个代码,我在论坛上的一个非常相似的问题中看到了这个代码。然而,当我编译它时,我的和代码的输出并没有返回任何东西。它只是空白。我可以得到一些关于我遗失或做错的反馈吗?谢谢。 BTW我只能使用while循环。

  System.out.println("Enter an integer:");
  int firstNum = keyboard.nextInt();

  System.out.println("Enter another integer larger than the first one:");
  int secondNum = keyboard.nextInt();

  System.out.println();

  int mod = firstNum % 2;
  int sum = 0;

  if (mod != 0)
  {
    firstNum++;
  }

  System.out.print("Even numbers: ");
  while (firstNum <= secondNum)
  {
    System.out.print(firstNum + " ");
    firstNum += 2;
  }

  System.out.println();

  System.out.print("Sum of even numbers: ");
  while (firstNum <= secondNum)
  {
    System.out.print(sum);
    sum += firstNum;
    firstNum += 2;
  }

3 个答案:

答案 0 :(得分:0)

在您第一次使用时,您已经更改了firstNum的值。所以基本上,当你到达第二个时,你甚至不进入它。 我只需更改第一个循环并删除第二个循环:

while (firstNum <= secondNum) {
        System.out.print(firstNum + " ");
        sum += firstNum;
        firstNum += 2;
    }

    System.out.println();

    System.out.print("Sum of even numbers: ");
    System.out.println(sum);

答案 1 :(得分:0)

在你的第一个while循环中 -

while (firstNum <= secondNum){
    System.out.print(firstNum + " ");
    firstNum += 2;
}

您继续递增变量firstNum,直到达到secondNum,因此第二个while循环(firstNum <= secondNum)的条件永远不会计算为true。

实现目标的一种非常简单的方法如下:

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
int total = 0;
while(firstNum <= secondNum){
    if(firstNum%2 == 0)
        total += firstNum;
    firstNum++;
}
System.out.println("Sum: " + total);

答案 2 :(得分:0)

第一个循环只打印偶数,因为firstNum到达secondNum所以无法添加。 如果你想保留相同的代码,你只需要添加一个打印偶数的新变量。因此第一个循环将打印偶数,第二个循环将执行总和。

例如:

int evenNumber;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();

System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();

System.out.println();

int mod = firstNum % 2;
int sum = 0;

if (mod != 0) {
    firstNum++;
}

evenNumber = firstNum;
System.out.print("Even numbers: ");
while (evenNumber <= secondNum) {
    System.out.print(evenNumber + " ");
    evenNumber += 2;
}

System.out.println();

System.out.print("Sum of even numbers: ");

while (firstNum <= secondNum) {
    sum += firstNum;
    firstNum += 2;
}
System.out.println(sum);

此代码不是最佳代码,但它符合您的需要。但如果你想做得更好,你可以这样做:

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
int total = 0;
System.out.print("Even Numbers: ");
while (firstNum <= secondNum) {
    if (firstNum % 2 == 0) {
        System.out.print(firstNum + " ");
        total += firstNum;
    }
    firstNum++;
}
System.out.println("\nSum: " + total);