简体   繁体   English

Java嵌套循环循环

[英]Java Nested Do-While Loops

So I have created this program that is suppose to compare two rectangles, a reference rectangle and a test rectangle to check if the test is within the reference, or the test is overlapping the reference, or if they share a boundary, or if they are distinct. 因此,我创建了一个程序,假设该程序可以比较两个矩形,一个参考矩形和一个测试矩形,以检查测试是否在参考范围内,或者测试是否与参考重叠,或者它们是否共享边界,或者不同。

My program must read in a reference rectangle first and then the test rectangle, but after the program completes the user should be able to choose whether to repeat either the whole program and enter a new reference rectangle or to repeat just the part where they enter the test rectangle. 我的程序必须先读取一个参考矩形,然后再读取测试矩形,但是在程序完成后,用户应该能够选择是重复整个程序并输入一个新的参考矩形,还是仅重复输入它们的部分。测试矩形。

I created a large reference while loop that encompassed the second testing while loop and made them have different repeatInt variables. 我创建了一个大型的while循环参考,其中包含第二个测试while循环,并使它们具有不同的repeatInt变量。 I am not sure where my issue is because when the user enters 2 the program correctly restarts at the testing rectangle input portion, but when they enter 1 the program again restarts at the testing input portion when it should go back to the beginning to the reference input portion and run the whole program again. 我不确定我的问题出在哪里,因为当用户输入2时,程序会在测试矩形输入部分正确地重新启动,但是当他们输入1时,程序应在测试输入部分中再次重新启动,此时应返回到引用的开头输入部分,然后再次运行整个程序。

Here is my code: 这是我的代码:

 import java.util.Scanner;

public class TestMyRectangle2D {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    MyRectangle2D refrec = new MyRectangle2D();
    MyRectangle2D testrec = new MyRectangle2D();

    double refx;
    double refy;
    double refwidth;
    double refheight;

    double testx;
    double testy;
    double testwidth;
    double testheight;

    double pointx;
    double pointy;

    int repeatInt = 1;
    int repeatInt2 = 1;

    boolean ContinueRefWidthLoop = true;
    boolean ContinueRefHeightLoop = true;
    boolean ContinueTestWidthLoop = true;
    boolean ContinueTestHeightLoop = true;

    System.out.println("This program will ask you to enter the center coordinates, height, and width of the reference rectangle which will be the reference for the following comparisons with an additional test rectangle and test point. The program will be able to tell you if the point you enter is within the reference rectangle, it will also test the test rectangle to see if it is either contained, overlapping, abut, or distinct with the reference rectangle");

    //Reference loop for rectangle
    while(repeatInt == 1){
        System.out.println("Please enter a numerical value for the reference rectangle's center x coordinate");
        refx = in.nextDouble();
        refrec.setX(refx);
        System.out.println("Please enter a numerical value for the reference rectangle's center y coordinate");
        refy = in.nextDouble();
        refrec.setY(refy);
        do{
            try {
                System.out.println("Please enter a numerical value for the reference rectangle's width");
                refwidth = in.nextDouble();
                refrec.setWidth(refwidth);

                ContinueRefWidthLoop = false;
            }
            catch (Exception e){
                System.out.println(refrec.getErrorMessage());
                ContinueRefWidthLoop = true;
            }
        } while (ContinueRefWidthLoop);

        do{
            try{
                System.out.println("Please enter a numerical value for the reference rectangle's height");
                refheight = in.nextDouble();
                refrec.setHeight(refheight);

                ContinueRefHeightLoop = false;
            }
            catch (Exception e){
                System.out.println(refrec.getErrorMessage());
                ContinueRefHeightLoop = true;
            }
        }while(ContinueRefHeightLoop);

        while(repeatInt2 == 1){

        System.out.println("Please enter a numerical value for the test rectangle's center x coordinate");
        testx = in.nextDouble();
        testrec.setX(testx);
        System.out.println("Please enter a numerical value for the test rectangle's center y coordinate");
        testy = in.nextDouble();
        testrec.setY(testy);

        do {
            try{
                //Testing loop for test point and rectangle
                System.out.println("Please enter a numerical value for the test rectangle's width");
                testwidth = in.nextDouble();
                testrec.setWidth(testwidth);

                ContinueTestWidthLoop = false;
            }
            catch (Exception e){
                System.out.println(testrec.getErrorMessage());
                ContinueTestWidthLoop = true;
            }
        } while(ContinueTestWidthLoop);

        do{
            try{
                System.out.println("Please enter a numerical value for the test rectangle's height");
                testheight = in.nextDouble();
                testrec.setHeight(testheight);

                ContinueTestHeightLoop = false;
            }
            catch (Exception e){
                System.out.println(testrec.getErrorMessage());
                ContinueTestHeightLoop = true;
            }
        }while(ContinueTestHeightLoop);

        //takes in the test point coordinates
        System.out.println("Please enter a numerical value for the test point's x coordinate");
        pointx = in.nextDouble();
        System.out.println("Please enter a numerical value for the test point's y coordinate");
        pointy = in.nextDouble();

        //perform all the checks the entered point and rectangle against the reference rectangle
        //checks if test point within reference rectangle
        System.out.println("The reference rectangle's values; " + "\n" + "Center coordinates: (" + refrec.getX() + ", " + refrec.getY() + ")" + "\n" + "Width: " + refrec.getWidth() + "\n" + "Height is: "+ refrec.getHeight() + "\n" + "Area:" + refrec.getArea() + "\n" + "Perimeter: " + refrec.getPerimeter());
        System.out.println("The test point coordinates are: (" + pointx + ", " + pointy + ")");
        if(refrec.contains(pointx, pointy)){
            System.out.println("----The test point is contained within the reference rectangle!");
        }
        else{
            System.out.println("----The test point is not contained within the reference rectangle");
        }

        System.out.println("The test rectangle's values; " + "\n" + "Center coordinates: (" + testrec.getX() + ", " + testrec.getY() + ")" + "\n" + "Width is: " + testrec.getWidth() + "\n" + "Height is: "+ testrec.getHeight() + "\n" + "Area:" + testrec.getArea() + "\n" + "Perimeter: " + testrec.getPerimeter());
        //checks if test rectangle is within the reference rectangle
        if(refrec.contains(testrec)){
            System.out.println("----The test rectangle is within the reference rectangle!");
        }
        else{
            System.out.println("----The test rectangle is not within the reference rectangle");
        }
        //checks if test rectangle is overlapping the reference rectangle only if the test rectangle is not contained within the reference rectangle
        if(refrec.overlaps(testrec)){
            System.out.println("----The test rectangle is overlapping the reference rectangle!");
        }
        else{
            System.out.println("----The test rectangle is not overlapping the reference rectangle");
        }

        //checks if test rectangle is sharing the boundary with the reference rectangle
        if(refrec.abut(testrec)){
            System.out.println("----The test rectangle is sharing a boundary with the reference rectangle!");
        }
        else{
            System.out.println("----The test rectangle is not sharing a boundary with the reference rectangle");
        }

        //checks if test rectangle is distinct to the reference rectangle
        if(refrec.distinct(testrec)){
            System.out.println("----The test rectangle and the reference rectangle are distinct!");
        }
        else{
            System.out.println("----The test rectangle and the reference rectangle are not distinct");
        }

        //program loop repeat 
        System.out.println("To reenter a new reference triangle enter 1, to reenter a new test point and new test rectangle enter 2, or to end the program altogether enter 0");

        switch (in.nextInt()) {
        case 1: {
            repeatInt = 1;
            break;
        }
        case 2: {
            repeatInt2 = 1;
            break;
        }
        default: {
            repeatInt = repeatInt2 = 0;
        }
        }
        }
    }       
}
}

Any help is greatly appreciated, thank you! 任何帮助,不胜感激,谢谢!

Both repeatInt and reapeatInt2 are both set to 1 and only will be changed if the user selects to end the program. repeatIntreapeatInt2都设置为1,并且只有在用户选择结束程序时才会更改。 You need to change these values at the end when they are picking how to restart. 在选择如何重新启动时,您需要在最后更改这些值。

switch (in.nextInt()) {
    case 1: {
        repeatInt = 1;
        repeatInt2 = 0; // add this line here
        break;
    }
    case 2: {
        repeatInt2 = 1;
        break;
    }
    default: {
        repeatInt = repeatInt2 = 0;
    }
}

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

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