有没有办法缩短这段 Java 代码?

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

Is there anyway to shorten this Java code?

问题

以下是翻译后的内容:

有没有一种方法可以在一个 do while 循环中验证身高和体重?
有没有一种方法可以在一个 if 条件中同时测试 input.hasNextDouble() and height > 5 && height <= 500

    do {
        System.out.print("身高(厘米):");
        input.nextLine();

        if (input.hasNextDouble()) {
            height = input.nextDouble();
            if (height > 5 && height <= 500) {
                isValid = true;
            } else {
                System.out.println("无效输入\n请重试\n");
                isValid = false;
            }
        } else {
            System.out.println("无效输入\n请重试\n");
            isValid = false;
        }
    } while (!(isValid));

    do {
        System.out.print("体重(公斤):");
        input.nextLine();

        if (input.hasNextDouble()) {
            weight = input.nextDouble();
            if (weight > 0 && weight <= 500) {
                isValid = true;
            } else {
                System.out.println("无效输入\n请重试\n");
                isValid = false;
            }
        } else {
            System.out.println("无效输入\n请重试\n");
            isValid = false;
        }
    } while (!(isValid));
英文:

Is there any way to validate height and weight in one do while?
Is there any way to test
input.hasNextDouble() and height &gt; 5 &amp;&amp; height &lt;= 500
together on an if condition?

    do {
            System.out.print(&quot;Height(cm): &quot;);
            input.nextLine();
    
            if (input.hasNextDouble()) {
                height = input.nextDouble();
                if (height &gt; 5 &amp;&amp; height &lt;= 500) {
                    isValid = true;
                } else {
                    System.out.println(&quot;Invalid input\nPlease try again\n&quot;);
                    isValid = false;
                }
            } else {
                System.out.println(&quot;Invalid input\nPlease try again\n&quot;);
                isValid = false;
            }
        } while (!(isValid));
    
        do {
            System.out.print(&quot;Weight(kg): &quot;);
            input.nextLine();
    
            if (input.hasNextDouble()) {
                weight = input.nextDouble();
                if (weight &gt; 0 &amp;&amp; weight &lt;= 500) {
                    isValid = true;
                } else {
                    System.out.println(&quot;Invalid input\nPlease try again\n&quot;);
                    isValid = false;
                }
            } else {
                System.out.println(&quot;Invalid input\nPlease try again\n&quot;);
                isValid = false;
            }
        } while (!(isValid));

答案1

得分: 1

定义一个通用代码块到一个单一函数中并使用三元运算符来内联 if 条件

```java
	public static double takeInput(String heightOrWeight, int lowConstraint, int highConstraint) {
		Scanner input = new Scanner(System.in);
		double validDouble = 0;
		boolean isValid = false;
		
		while(!(isValid)){
	        System.out.print(heightOrWeight + ":");
	        input.nextLine();

	        validDouble = input.hasNextDouble() ? input.nextDouble(): Integer.MIN_VALUE;
	        
	        if (validDouble > lowConstraint && validDouble <= highConstraint) {
	        	isValid = true;
	        }
	     
	        if (!isValid) {
	        	 System.out.println("无效的输入\n请重试\n");
	        }
	    }
		return validDouble;
	}

使用适当的参数调用上述方法。

		double height = takeInput("身高(厘米)", 5, 500);
		double weight = takeInput("体重(公斤)", 0, 500);

<details>
<summary>英文:</summary>
Defining a common code into a single function and Using ternary operator to inline if condition. 
public static double takeInput(String heightOrWeight, int lowConstraint, int highConstraint) {
Scanner input = new Scanner(System.in);
double validDouble = 0;
boolean isValid = false;
while(!(isValid)){
System.out.print(heightOrWeight + &quot;: &quot;);
input.nextLine();
validDouble = input.hasNextDouble() ? input.nextDouble(): Integer.MIN_VALUE;
if (validDouble &gt; lowConstraint &amp;&amp; validDouble &lt;= highConstraint) {
isValid = true;
}
if (!isValid) {
System.out.println(&quot;Invalid input\nPlease try again\n&quot;);
}
}
return validDouble;
}

Calling above method with appropriate parameters.
	double height = takeInput(&quot;Height(cm)&quot;, 5,500);
double weight = takeInput(&quot;Weight(kg)&quot;, 0,500);

</details>

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

发表评论

匿名网友

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

确定