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

How can I get this loop to accept a positive integer for two entries?

为了更清楚,请协助我 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);

}
}

据我所知,如果我只需要实现你在问题中的要求,我可以简单地做。

(我在 JAVA 中给出了一个备用代码,您可以相应地修改您的 Japplet

int a=-1,b=-1;
a=sc.nextInt();
b=sc.nextInt();

if(a>=0 && b>=0){
  ...
  //do the task
}

else{
   while(a<0 || b<0){

      System.out.println("negative value not allowed, enter +ve value ");
      a=sc.nextInt();
      b=sc.nextInt();
   }

  // while exited only if value both a and b are positive
  //+ve value
  //perform task       
}

根据 first 的长度是负数还是正数,你有两个不同的代码块,这让你自己把事情搞得太复杂了。基本上,您的代码如下所示:

If the length is negative then {
    Ask for a new length until it's positive
    Now input the width, reject negative values, do the computation,
    and output it
} else { // the length is positive
    Input the width, reject negative values, do the computation,
    and output it
}

关于处理宽度的整个部分在您的代码中出现了两次,这使得代码变得不必要地复杂,并且更容易出错,例如将大括号放在错误的位置(我认为这就是代码的原因行为不端)。通过像这样重新组织代码,您可以让自己的生活变得更简单:

If the length is negative then {
    Ask for a new length until it's positive
}
// When we get here, the length will be positive.  It doesn't matter 
// whether we got here because the original length was positive, or whether
// it was negative and the user entered a new value.  We're going to 
// continue in the same way, either way.
Input the width, reject negative values, do the computation,
and output it

(顺便说一下,如果用户输入0,我不知道你想做什么。你真的没有处理这种情况。)

在第 48 行,您的语句 if(Double.parseDouble(unknownLength) < 0){ 在第 76 行具有匹配的右大括号,就在 } else if(menuChoice == 3){.

之前

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

您还应该尝试格式化您的代码。这将使它更具可读性,并且您可以很容易地看到在使用美化工具(例如 Tutorial Point Online Java Formatter.

后大括号没有排列在它们应该排列的位置