2016-08-06 4 views
0

두 개의 사각형, 참조 사각형 및 테스트 사각형을 비교하여 테스트가 참조 내에 있는지 테스트가 참조를 겹치고 있는지 또는이 참조가 겹치는 지 확인하는이 프로그램을 만들었습니다. 그들은 경계를 공유하거나 그것이 뚜렷한 경우.Java 중첩 된 Do-While 루프

내 프로그램은 먼저 참조 사각형을 읽은 다음 테스트 사각형을 읽어야하지만 프로그램이 완료된 후에 사용자는 전체 프로그램을 반복할지 또는 새로운 참조 사각형을 입력할지 또는 일부만 반복할지 여부를 선택할 수 있어야합니다 테스트 사각형에 입력합니다.

두 번째 testing while 루프를 포함하고 반복 repeat 변수를 다르게 만드는 큰 참조 while 루프를 만들었습니다. 사용자가 2를 입력하면 테스트 사각형 입력 부분에서 프로그램이 올바르게 다시 시작되기 때문에 문제가 어디에서 발생했는지 알 수는 없지만 1을 입력하면 테스트 입력 부분에서 프로그램이 다시 시작되어 참조로 돌아갑니다. 입력 부분을 선택하고 전체 프로그램을 다시 실행하십시오.

여기 내 코드입니다 :

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

어떤 도움이 크게 감사합니다, 감사합니다!

답변

0

repeatIntreapeatInt2 모두 1로 설정되며 사용자가 프로그램을 끝내기로 선택한 경우에만 변경됩니다. 끝에서 다시 시작하는 방법을 선택할 때이 값을 변경해야합니다.

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