简体   繁体   中英

JAVA How to generate a table of numbers in a specific range?

I'm curious as to how I should go about generating a vertical list of numbers in a specific range given by a user.

Say I have a user enter a low number and a high number and I want to display those numbers and every number between those numbers.

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

As you can probably tell, I am pretty lost.

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

Here is one of the ways:

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

    }
}

OUTPUT:

Enter the low number 
15
Enter the high number
20

15
16
17
18
19
20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM