JAVA如何生成特定范围内的数字表?

时间:2017-06-17 01:33:25

标签: java numbers range tabular

我很想知道如何在用户给出的特定范围内生成垂直数字列表。

假设我有一个用户输入一个低数字和一个高数字,我想显示这些数字和这些数字之间的每个数字。

import java.util.Scanner; 

public class Table {

public static void main(String[] args) {
    //Create Scanner
    Scanner input = new Scanner(System.in); 

    //Prompt user to enter low number 
    System.out.println("Enter the low number ");
    int lowNumber = input.nextInt(); 

    //Prompt user to enter high number 
    System.out.println("Enter the high number");
    int highNumber = input.nextInt(); 

    //Create table
    for (int i = 1; i <=256; i++){
        System.out.print(i);
    }

你可能会说,我很丢失。

2 个答案:

答案 0 :(得分:0)

IntStream.range(lowNumber, highNumber).forEach(i -> System.out.println(i));

答案 1 :(得分:0)

以下是其中一种方式:

import java.util.Scanner;

public class Table {

    public static void main(String[] args) {
        //Create Scanner
        Scanner input = new Scanner(System.in);

        //Prompt user to enter low number 
        System.out.println("Enter the low number ");
        int lowNumber = input.nextInt();

        //Prompt user to enter high number 
        System.out.println("Enter the high number");
        int highNumber = input.nextInt();
        System.out.println();

        //Create table
        for (int i = lowNumber; i <= highNumber; i++) {
            System.out.println(i);
        }

    }
}

<强>输出:

Enter the low number 
15
Enter the high number
20

15
16
17
18
19
20