简体   繁体   English

如何获得此循环以接受两个条目的正整数?

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

To be more clear, please assist me with menuChoice == 2. 为了更清楚,请通过menuChoice == 2帮助我。

I've gotten to the point where if you enter negative values, it prompts you to enter again until it's positive and then the calculations come out fine. 我已经到了要输入负值的地步,它会提示您再次输入,直到它为正,然后计算就可以了。 However, when I enter just positive values now, it doesn't calculate anything. 但是,当我现在只输入正值时,它什么也不会计算。 I've been trying for a while but I just can't figure it out. 我已经尝试了一段时间,但我无法弄清楚。

What do I need to do? 我需要做什么?

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);

}
}

On line 48, your statement if(Double.parseDouble(unknownLength) < 0){ has the matching closing brace is on line 76, just before the } else if(menuChoice == 3){ . 在第48行,您的语句if(Double.parseDouble(unknownLength) < 0){具有匹配的if(Double.parseDouble(unknownLength) < 0){括号在第76行,就在} else if(menuChoice == 3){

So, logically your code is only running the menuChoice == 2 section when a negative number is entered. 因此,从逻辑menuChoice == 2当输入负数时,您的代码仅运行menuChoice == 2部分。 You should instead close the if after the first do-while loop completes, since at that point the number will have been (corrected to) positive. 相反,您应该在第一个do-while循环完成后关闭if,因为此时该数字已被(校正为)正数。

You should also try to work on formatting your code. 您还应该尝试格式化代码。 It will make it more readable and you can easily see that the braces don't line up where they should have after using a beautifier tool, such as Tutorial Point Online Java Formatter . 它将使它更具可读性,并且在使用美化工具(例如Tutorial Point Online Java Formatter)之后,您可以轻松地看到花括号没有对齐在应有的位置。

You've made things too complicated for yourself by having two different blocks of code depending on whether the first length is negative or positive. 根据第一个长度是负数还是正数,使用两个不同的代码块,对您来说,事情变得太复杂了。 Basically, your code looks like: 基本上,您的代码如下所示:

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
}

The whole part about handling the width occurs twice in your code, which makes the code needlessly complex, and makes it more prone to errors such as getting the curly braces in the wrong place (which I think is why the code isn't behaving). 有关处理宽度的整个部分在代码中发生了两次,这使代码不必要地变得复杂,并使其更容易出错,例如将花括号放在错误的位置(我认为这是代码不起作用的原因) 。 You can make life a lot simpler for yourself by reorganizing the code like this: 通过重新组织如下代码,您可以使自己的生活简单得多:

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

(By the way, I don't know what you want to do if the user enters 0. You really aren't handling that case.) (顺便说一句,如果用户输入0,我不知道要做什么。您确实没有处理这种情况。)

As far as I could understand, if I have to just achieve what you are asking in the question ,I could simply do like. 据我了解,如果我只想实现您在问题中的要求,我可以简单地做到。

(I am giving just an alternate code here in JAVA , you can modify your Japplet accordingly) (我在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       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM