英文:
Java userInput Validation
问题
需要帮助的是代码的这部分,我对编程还不太熟悉,所以不太擅长。以下是问题:
要求用户输入一个双精度浮点数(数字),如果输入的值不是双精度浮点数,则继续要求用户输入,直到他们输入一个双精度浮点数为止。一旦他们输入了一个双精度浮点数,就检查输入的值是否在范围内(1-25之间)。将会持续提示直到 age1 在这个范围内。
Scanner input = new Scanner(System.in);
while (!input.hasNextDouble())
{
input.nextLine();
System.out.print("输入不是数字,请重新输入:");
}
double age1 = input.nextDouble();
while (age1 < 1 || age1 > 25)
{
System.out.print("请输入1-25之间的年龄:");
age1 = input.nextDouble();
}
英文:
I need help with this part of code i am fairly new at coding so i am not that good below is the question
Ask user to input a double(number), if the value entered is not a double it continues asking them until they enter a double. Once they enter a double it checks if the double entered is in the range (between 1-25). It will continue prompting until age1 is in that range.
Scanner input = new Scanner(System.in);
while(!input.hasNextDouble())
{
input.nextLine();
System.out.print("Input is not a number ReEnter:");
}
double age1 = input.nextDouble();
while (age1 < 1 || age1 > 25)
{
System.out.print( "Enter the Age between 1-25 Only!");
age1 = input.nextDouble();
}
答案1
得分: 2
你的代码运行得很好。尽管在第二个while循环后不会检查输入的是否是有效的双精度数,所以最好将验证合并到一个单独的循环中:
Scanner input = new Scanner(System.in);
while (true) {
if (!input.hasNextDouble()) {
input.nextLine();
System.out.print("输入不是数字,请重新输入:");
} else {
double age1 = input.nextDouble();
if (age1 < 1 || age1 > 25) {
System.out.print("请输入1到25之间的年龄!");
input.nextLine();
} else {
break;
}
}
}
英文:
Your code runs just fine. Though it won't check if a valid double is entered after it reaches the second while loop, so it's best you combine the validation in a single loop:
Scanner input = new Scanner(System.in);
while(true)
{
if(!input.hasNextDouble()) {
input.nextLine();
System.out.print("Input is not a number ReEnter:");
}else {
double age1 = input.nextDouble();
if (age1 < 1 || age1 > 25){
System.out.print( "Enter the Age between 1-25 Only!");
input.nextLine();
}else {
break;
}
}
}
答案2
得分: 1
当您需要检查范围时,请使用 &&
,因为我们需要它在这些范围之间<br>
此外,您的条件也较弱while (age1 < 1 || age1 > 25)
应该是 <br>
while (age1 >= 1 && age1 <= 25)
使用 wrapper
类将字符串转换为 double
。
Scanner sc = new Scanner(System.in);
System.out.print("Enter a double number:");
String d = sc.next();
while(true) {
boolean dot = false;
for(int i = 0 ; i < d.length() ; i++) {
char ch = d.charAt(i);
// 检查小数点
if(ch == '.') {
dot = true;
break;
}
}
if(dot == false) {
System.out.print("Input is not a double number ReEnter:");
d = sc.next();
}else {
// 包装类
double age = Double.parseDouble(d);
if(age >= 1 && age <= 25) {
System.out.println("Just prefect");
break;
}else {
System.out.print( "Enter the Age between 1-25 Only!");
d = sc.next();
}
}// else 块
}//while 块
输出:
Enter a double number:1
Input is not a double number ReEnter:20
Input is not a double number ReEnter:26.00
Enter the Age between 1-25 Only!15
Input is not a double number ReEnter:15.0
Just prefect
英文:
When you need to check range use &&
cause we need it in between those range<br>
Additionally your condition weak toowhile (age1 < 1 || age1 > 25)
it should be <br>
while (age1 >= 1 && age1 <= 25)
Using wrapper
class to convert String to double
.
Scanner sc = new Scanner(System.in);
System.out.print("Enter a double number:");
String d = sc.next();
while(true) {
boolean dot = false;
for(int i = 0 ; i < d.length() ; i++) {
char ch = d.charAt(i);
// checking for a decimal 'dot'
if(ch == '.') {
dot = true;
break;
}
}
if(dot == false) {
System.out.print("Input is not a double number ReEnter:");
d = sc.next();
}else {
// wrapper class
double age = Double.parseDouble(d);
if(age >= 1 && age <= 25) {
System.out.println("Just prefect");
break;
}else {
System.out.print( "Enter the Age between 1-25 Only!");
d = sc.next();
}
}// else block
}//while block
Output:
Enter a double number:1
Input is not a double number ReEnter:20
Input is not a double number ReEnter:26.00
Enter the Age between 1-25 Only!15
Input is not a double number ReEnter:15.0
Just prefect
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论