Java 扫描仪验证的多个while循环条件
大家好,我正在用java验证用户输入的整数范围。我是编程和java新手。我遇到的困难是倍数&接近尾声。我不知道该怎么做,所以我猜到了。我在别处找不到合适的解决办法Java 扫描仪验证的多个while循环条件,java,loops,while-loop,Java,Loops,While Loop,大家好,我正在用java验证用户输入的整数范围。我是编程和java新手。我遇到的困难是倍数&接近尾声。我不知道该怎么做,所以我猜到了。我在别处找不到合适的解决办法 import java.util.Scanner; class Main { public static void main(String[] args) { // Create a Scanner Scanner sc = new Scanner(System.in); int nu
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// Create a Scanner
Scanner sc = new Scanner(System.in);
int number;
do {
System.out.println("Please enter window width");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
number = sc.nextInt();
} while ((width > 0.5 & <2.5) && (height >0.5 & <3.5));
System.out.println("Window is:" + height + "m high " + width + "m wide.");
}
import java.util.Scanner;
班长{
公共静态void main(字符串[]args){
//创建扫描仪
扫描仪sc=新的扫描仪(System.in);
整数;
做{
System.out.println(“请输入窗口宽度”);
而(!sc.hasnetint()){
System.out.println(“那不是一个数字!”);
sc.next();//这很重要!
}
编号=sc.nextInt();
}虽然((宽度>0.5&0.5&您的do-while循环语法似乎不正确,请尝试以下代码:
do {
System.out.println("Please enter window width");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
number = sc.nextInt();
}
while((width > 0.5 && width < 2.5) && (height > 0.5 && height < 3.5));
do{
System.out.println(“请输入窗口宽度”);
而(!sc.hasnetint()){
System.out.println(“那不是一个数字!”);
sc.next();//这很重要!
}
编号=sc.nextInt();
}
而((宽度>0.5和宽度<2.5)和((高度>0.5和高度<3.5));
使用Double#parseDouble
解析输入,如果解析不成功(即抛出异常),则返回请求用户重试。验证输入(是否为数字)后,根据您的要求验证范围
按如下方式操作:
import java.util.Scanner;
class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Variables for width and height
double width, height;
// Input width
do {
width = getDimennsion("Please enter window width between 0.5 & 2.5: ");
} while (width < 0.5 || width > 2.5);// Loop back in case of invalid dimension
// Input height
do {
height = getDimennsion("Please enter window height between 0.5 & 3.5: ");
} while (height < 0.5 || height > 3.5);// Loop back in case of invalid dimension
System.out.println("Width: " + width + ", Height: " + height);
// ...Rest of the processing
}
static double getDimennsion(String msg) {
boolean valid;
double num = 0;
do {
valid = true;
System.out.print(msg);
try {
// Get input
num = Double.parseDouble(sc.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("Invalid input. Please try again");
valid = false;
}
} while (!valid);// Loop back in case of invalid input
return num;
}
}
while((宽度>0.5&&width<2.5)和&(高度>0.5&&height<3.5))
澄清:&
执行按位AND比较,例如0b1001&0b1010==0b1000
&
是布尔AND,如果两个操作数都为真,则返回true。宽度和高度在循环中保持不变。建议您发布相关代码,而不是发布整个演示class.对于演示,您可以提供。它将使您的答案简短。@Code\u模式-我不喜欢使用第三方在线工具发布代码,因为该在线工具不受SO的控制,可能会被关闭。
Please enter window width between 0.5 & 2.5: a
Invalid input. Please try again
Please enter window width between 0.5 & 2.5: 10
Please enter window width between 0.5 & 2.5: 2
Please enter window height between 0.5 & 3.5: b
Invalid input. Please try again
Please enter window height between 0.5 & 3.5: 15
Please enter window height between 0.5 & 3.5: 3
Width: 2.0, Height: 3.0