英文:
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 > 5 && height <= 500
together on an if
condition?
do {
System.out.print("Height(cm): ");
input.nextLine();
if (input.hasNextDouble()) {
height = input.nextDouble();
if (height > 5 && height <= 500) {
isValid = true;
} else {
System.out.println("Invalid input\nPlease try again\n");
isValid = false;
}
} else {
System.out.println("Invalid input\nPlease try again\n");
isValid = false;
}
} while (!(isValid));
do {
System.out.print("Weight(kg): ");
input.nextLine();
if (input.hasNextDouble()) {
weight = input.nextDouble();
if (weight > 0 && weight <= 500) {
isValid = true;
} else {
System.out.println("Invalid input\nPlease try again\n");
isValid = false;
}
} else {
System.out.println("Invalid input\nPlease try again\n");
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 + ": ");
input.nextLine();
validDouble = input.hasNextDouble() ? input.nextDouble(): Integer.MIN_VALUE;
if (validDouble > lowConstraint && validDouble <= highConstraint) {
isValid = true;
}
if (!isValid) {
System.out.println("Invalid input\nPlease try again\n");
}
}
return validDouble;
}
Calling above method with appropriate parameters.
double height = takeInput("Height(cm)", 5,500);
double weight = takeInput("Weight(kg)", 0,500);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论