Java用户输入验证

huangapple go评论69阅读模式
英文:

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(&quot;Input is not a number ReEnter:&quot;);
 }
 double age1 = input.nextDouble();
 while (age1 &lt; 1 || age1 &gt; 25)
 {
 System.out.print( &quot;Enter the Age between 1-25 Only!&quot;);
       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(&quot;Input is not a number ReEnter:&quot;);
		    }else {		    
			    double age1 = input.nextDouble();
				if (age1 &lt; 1 || age1 &gt; 25){
					System.out.print( &quot;Enter the Age between 1-25 Only!&quot;);
				    input.nextLine();
				}else {
					break;
				}
		    }
		 }

答案2

得分: 1

当您需要检查范围时,请使用 &amp;&amp;,因为我们需要它在这些范围之间<br>
此外,您的条件也较弱while (age1 &lt; 1 || age1 &gt; 25) 应该是 <br>
while (age1 &gt;= 1 &amp;&amp; age1 &lt;= 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 &lt; 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 &amp;&amp; cause we need it in between those range<br>
Additionally your condition weak toowhile (age1 &lt; 1 || age1 &gt; 25) it should be <br>
while (age1 &gt;= 1 &amp;&amp; age1 &lt;= 25)

Using wrapper class to convert String to double.

            Scanner sc = new Scanner(System.in);
            System.out.print(&quot;Enter a double number:&quot;);
            String d = sc.next();
            while(true) {
            	boolean dot = false;
            	for(int i = 0 ; i &lt; d.length() ; i++) {
            		char ch = d.charAt(i);
                    // checking for a decimal &#39;dot&#39;
            		if(ch == &#39;.&#39;) {
            			dot = true;
            			break;
            		}
            	}
            	if(dot == false) {
	            	System.out.print(&quot;Input is not a double number ReEnter:&quot;);
	            	d = sc.next();
            	}else {
            		// wrapper class
            		double age = Double.parseDouble(d);
            		if(age &gt;= 1 &amp;&amp; age &lt;= 25) {
            			System.out.println(&quot;Just prefect&quot;);
            			break;
            		}else {
            			System.out.print( &quot;Enter the Age between 1-25 Only!&quot;);
            			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

huangapple
  • 本文由 发表于 2020年10月10日 08:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64288801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定