gpt4 book ai didi

java - 我怎样才能让这个循环接受两个条目的正整数?

转载 作者:行者123 更新时间:2023-12-01 21:16:22 25 4
gpt4 key购买 nike

为了更清楚,请协助我使用 menuChoice == 2。

我已经达到这样的程度:如果您输入负值,它会提示您再次输入,直到输入为正值,然后计算结果正常。但是,当我现在只输入正值时,它不会计算任何内容。我已经尝试了一段时间,但就是无法弄清楚。

我需要做什么?

package finalExam;

//this is required for JOptionPane to work
import javax.swing.JOptionPane;

public class Geometry {

public static void main(String[] args) {


boolean valid = false;

int menuChoice;

do {
// create a menu and display it to the user
// then ask the user to choose an option
String menu = "1) Calculate the area of a circle\n"
+ "2) Calculate the area of a rectangle\n"
+ "3) Calculate the area of a triangle\n"
+ "4) Quit\n"
+ "Please enter your choice: (1, 2, 3, or 4)";

menuChoice = Integer.parseInt(JOptionPane.showInputDialog(menu));

if(menuChoice == 1)
{
String unknownRadius = JOptionPane.showInputDialog("What is the radius of the circle?");
if(Double.parseDouble(unknownRadius) < 0){
do{
JOptionPane.showMessageDialog(null, "Please enter positive numbers only.");
unknownRadius = JOptionPane.showInputDialog("What is the radius of the circle?");
}
while(Double.parseDouble(unknownRadius) < 0);
double knownRadius = Double.parseDouble(unknownRadius);
double circleArea = Math.pow(knownRadius, 2) * 3.14159;
JOptionPane.showMessageDialog(null, "The area of the circle is " + circleArea);
}
else if(Double.parseDouble(unknownRadius) > 0) {
double knownRadius = Double.parseDouble(unknownRadius);
double circleArea = Math.pow(knownRadius, 2) * 3.14159;
JOptionPane.showMessageDialog(null, "The area of the circle is " + circleArea);
valid = true;
}

} else if(menuChoice == 2){
String unknownLength = JOptionPane.showInputDialog("What is the length of the rectangle?");
if(Double.parseDouble(unknownLength) < 0){
do{
JOptionPane.showMessageDialog(null, "Please enter positive numbers only.");
unknownLength = JOptionPane.showInputDialog("What is the length of the rectangle?");
}
while(Double.parseDouble(unknownLength) < 0);
double knownLength = Double.parseDouble(unknownLength);
String unknownWidth = JOptionPane.showInputDialog("What is the width of the rectangle?");
if(Double.parseDouble(unknownWidth) < 0){
do{
JOptionPane.showMessageDialog(null, "Please enter positive numbers only.");
unknownWidth = JOptionPane.showInputDialog("What is the width of the rectangle?");
}
while(Double.parseDouble(unknownWidth) < 0);
double knownWidth = Double.parseDouble(unknownWidth);
double rectangleArea = knownLength * knownWidth;
JOptionPane.showMessageDialog(null, "The area of the rectangle is " + rectangleArea);
}
else if(Double.parseDouble(unknownLength) > 0){
knownLength = Double.parseDouble(unknownLength);
unknownWidth = JOptionPane.showInputDialog("What is the width of the rectangle?");
if(Double.parseDouble(unknownWidth) > 0) {
double knownWidth = Double.parseDouble(unknownWidth);
double rectangleArea = knownLength * knownWidth;
JOptionPane.showMessageDialog(null, "The area of the rectangle is " + rectangleArea);
valid = true;
}
}
}

} else if(menuChoice == 3){
String unknownBase = JOptionPane.showInputDialog("What is the base length of the triangle?");
if(Double.parseDouble(unknownBase) > 0){
double knownBase = Double.parseDouble(unknownBase);
String unknownHeight = JOptionPane.showInputDialog("What is the height of the triangle?");
if(Double.parseDouble(unknownHeight) > 0){
double knownHeight = Double.parseDouble(unknownHeight);
double triangleArea = (knownBase / 2) * knownHeight;
JOptionPane.showMessageDialog(null, "The area of the triangle is " + triangleArea);
valid = true;
}
else { JOptionPane.showMessageDialog(null, "Please enter a positive number");
JOptionPane.showInputDialog("What is the base length of the triangle?");
}
}
else { JOptionPane.showMessageDialog(null, "Please enter a positive number");
JOptionPane.showInputDialog("What is the height of the triangle?");
}

}else if(menuChoice == 4){
System.exit(0);

} else
JOptionPane.showMessageDialog(null, "Please select from the options given (1-4)!");
}

while(!valid || menuChoice != 4);

}
}

最佳答案

第 48 行,您的陈述 if(Double.parseDouble(unknownLength) < 0){匹配的右大括号位于第 76 行,就在 } else if(menuChoice == 3){ 之前.

因此,从逻辑上讲,您的代码仅运行 menuChoice == 2输入负数时的部分。您应该在第一个 do-while 循环完成后关闭 if,因为此时数字将已(更正为)正值。

您还应该尝试格式化代码。这将使它更具可读性,并且您可以轻松地看到使用美化工具后大括号没有排列在应有的位置,例如 Tutorial Point Online Java Formatter .

关于java - 我怎样才能让这个循环接受两个条目的正整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40012786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com