Java 编写一个程序来读取两个数字,第一个小于第二个。该程序将写入两个数字之间的所有数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19528826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:03:24  来源:igfitidea点击:

Write a program to read two numbers, the first less than the second. The program will write all numbers between the two numbers

javawhile-loopcounterjgrasp

提问by

//So I wrote this: (the output down there)

//所以我写了这个:(输出在那里)

import java.util.Scanner;
public class E7L6{
public static void main(String[]args){
int num1, num2;
Scanner keyboard= new Scanner(System.in);
System.out.print("Type two numbers:");
num1= keyboard.nextInt();
num2= keyboard.nextInt();

if(num1<num2){
   while(num1<=num2){
   int counter=num1;
   System.out.print(counter+" "+num2);
   counter=counter+1;
   }}
else{
   System.out.print("Error: the first number must be smaller than the second");
}   
  }}

Output:
 ----jGRASP exec: java E7L6
Type two numbers:4 124
 4 4 4 4 4 4 4 4 4
 ----jGRASP: process ended by user.

//so many number 4 repeating can someone tell me where am i wrong? thanks!!

//这么多重复的数字4有人能告诉我我错在哪里了吗?谢谢!!

采纳答案by Reji

  while(num1<=num2){
   int counter=num1;
   System.out.print(counter+" "+num2);
   counter=counter+1;
  }

This will end up in infinite loop. The condition for the while loop needs to be modified.

这将最终陷入无限循环。需要修改 while 循环的条件。

// modified code - Complete code.

// 修改后的代码 - 完整的代码。

import java.util.Scanner;

public class E7L6 {
    public static void main(String[] args) {
        int num1, num2;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type two numbers:");
        num1 = keyboard.nextInt();
        num2 = keyboard.nextInt();

        if (num1 < num2) {
            int counter = num1;
            while (counter <= num2) {
                System.out.print(counter + " ");
                counter = counter + 1;
            }
        } else {
            System.out
                    .print("Error: the first number must be smaller than the second");
        }
    }
}

回答by SLaks

int counter=num1;

You created a new countervariable for each iteration of the loop.
Therefore, they all have the same value.

counter为循环的每次迭代创建了一个新变量。
因此,它们都具有相同的值。

It runs forever because your loop condition can never be false (you never change num1or num2).

它永远运行,因为您的循环条件永远不会为假(您永远不会改变num1num2)。

回答by Kai

As a novice programmer I would use for loop instead. It would be simpler and easier:

作为新手程序员,我会改用 for 循环。它会更简单,更容易:

for(ctr=num1;ctr<=num2;ctr++)
   s.o.p(ctr)

回答by Dvoryanin25

Here you go:

干得好:

System.out.print("First:");
    int first = Integer.parseInt(reader.nextLine());
    System.out.print("Second:");
    int second = Integer.parseInt(reader.nextLine());
    while (first <= second) {
        System.out.println(first);
        first++;
    }