简体   繁体   English

如何添加到 java 代码,要求用户输入 1 到 1000 之间的金额

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

Java program that will: Java 程序将:

  1. User can input an amount from 1.00 to 1,000.00用户可以输入 1.00 到 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);
    }
}
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注意:你指定的数字范围对应的是double类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何要求用户使用Java代码输入要加密的文本 - how to ask the user to enter a text for encryption in java code 我将如何在 java 中创建一个循环,要求用户从数组列表中选择一个 class 并且如果他们输入无效值再次询问? - How would I make a loop in java that will ask the user to pick a class from the array list and if they enter an invalid value to ask again? 如何创建数组以要求用户输入一定数量的数字,然后取平均值 - How to create array to ask user to enter a certain amount of numbers and then take the average 哪个级别的记录器适合要求用户在 java 中输入输入? - Which level of the logger is appropriate to ask the user to enter an input in java? 如何限制用户可以输入的字符数? - How to restrict amount of characters a user can enter? 如何要求用户输入哈希表的键并返回对象值 - How to ask user to enter a key for a hashtable and return the object value 我如何要求用户重新输入他们的选择? - How can I ask the user to re-enter their choice? 如何允许用户在包含String的java代码中输入文本? - How to allow user to enter a text in java code containing a String? 如何编码 java 使用方法显示和用户可以输入 - how to code java that using method to display and user can enter input 如何在下面的代码中使用循环,以便在用户再次输入任何字母程序时要求输入数字而不是终止程序? - how can loop be used in following code so that if user enters any alphabet program again ask to enter a nuumber rahter than terminating a program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM