[英]How to add to java code that will ask user to enter an amount from 1 to 1000
Java program that will: Java 程序将:
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.