user16621915
user16621915

Reputation:

How to add to java code that will ask user to enter an amount from 1 to 1000

Java program that will:

  1. User can input an amount from 1.00 to 1,000.00
  2. Any amount beyond the allowed range will display an error.

This block of code below is prints the number entered by an user:

import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) {

        // Creates a reader instance which takes
        // input from standard input - keyboard
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter a number: ");

        // nextInt() reads the next integer from the keyboard
        int number = reader.nextInt();

        // println() prints the following line to the output screen
        System.out.println("You entered: " + number);
    }
}

Upvotes: 2

Views: 275

Answers (1)

ElishevaMiler
ElishevaMiler

Reputation: 46

public static void main(String[] args)
    {
        Scanner reader = new Scanner(System.in);
        double number;
            System.out.print("Enter a number from 1.00 to 1,000.00: ");
            number= reader.nextDouble();
        //As long as the input is not within the range requests a new input
        while (!(number>=1.00 && number<=1000.00)){
            System.out.print("Input arrival!\nEnter a number from 1.00 to 1,000.00: ");
            number= reader.nextDouble();
        }
        System.out.println("You entered: " + number);
    }

note: The number range you specified corresponds to a double type

Upvotes: 1

Related Questions