声明正在检查但允许无效输入

时间:2014-11-14 16:41:06

标签: java math while-loop geometry error-checking

我正在尝试编写一个程序,该程序读取3个数字作为三角形边长。这些数字必须代表一个可能的三角形,如果输入3个零,它将终止该程序。我有点工作。虽然我很确定我有一段时间检查它,但它在开始时输入3个零而不是之后输入。同样在提供的图片中,这些值不起作用。

 *Program Description:
 *This program reads an arbitrary number of sets of triangle sides using only integer values. The progam will: 
 *Prompt the user for sets of numbers and process them until the user submits the numbers 0 0 0, which will terminate the program. 
 *For each set of three numbers, the program will print the values read. 
 *For each set of three numbers, the program will decide if the numbers represent the sides of a valid triangle. 
 *If the numbers can not represent a valid triangle, it will display an appropriate error message. 
 *If the numbers are valid, the program will determine, and display, the: 
 *side classification of the triangle – equilateral, isosceles, or scalene 
 *angle classification of the triangle – right, acute, or obtuse*/
import java.util.Scanner;
class triangleType
{
  public static void main (String [] args)
  {
    int side1 = -6, side2 = -1, side3 = -1; 
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter three integers that represent VALID sides of a triangle. Enter 0 0 0 to terminate the program.");
    side1 = in.nextInt();
    side2 = in.nextInt();
    side3 = in.nextInt();
    while (side1!=0&&side2!=0&&side3!=0)//checks if the user entered 3 zeros to skip the loop and terminate the program
    {
      while ((side1<=0||side2<=0||side3<=0))
      {
        System.out.println("You have entered at least one invalid value. This means your values could not make a triangle. Please enter new values.");
        side1 = in.nextInt();
        side2 = in.nextInt();
  side3 = in.nextInt();
      }
      while ((side1>=side2+side3||side2>=side1+side3||side3>=side2+side1))//checks if side values entered by user are valid
      {
        System.out.println("You have entered at least one invalid value. This means your values could not make a triangle. Please enter new values.");
        side1 = in.nextInt();
        side2 = in.nextInt();
        side3 = in.nextInt();
        if (side1==0&&side2==0&&side3==0)//checks if the user entered 3 zeros to break the loop and terminate the program
        {
          break;
        }
      }
    }
    if (side1==0&&side2==0&&side3==0)//checks if the user entered 3 zeros to terminate the program, if not the program will run
    {
      System.out.println("You have chosen to terminate the program.");
    }
    else
    {
      System.out.println("Your side lengths are valid. You entered: " + side1 + ", " + side2 + " and " + side3);
      if((isIsosceles (side1, side2, side3)) == true)
      {
        System.out.println("Side classification of the triangle: Isosceles"); 
      }
      else if((isEquilateral (side1, side2, side3)) == true)
      {
        System.out.println("Side classification of the triangle: Equilateral");
      }
      else
      {
System.out.println("Side classification of the triangle: Scalene");
      }
      if ((isAcute (side1, side2, side3)) == true)
      {
        System.out.println("Angle classification of the triangle: Acute");
      }
      else
      {
        System.out.println("Angle classification of the triangle: Obtuse");
      }
    }
  }

  public static boolean isEquilateral (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is equilateral
   Returns boolean result of whether or not the triangle is equilateral.*/
  {
    if ((s1==s2)&&(s2==s3))//checks to see if all sides are equal, if so it will run and return true if not it will return false
    {
      return true;
    }
    else 
    {
      return false;
    }
  }

  public static boolean isIsosceles (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is isosceles
   Returns boolean result of whether or not the triangle is isosceles.*/
  {
    if (((s1==s2)&&s1!=s3)||((s1==s3)&&s1!=s2)||((s2==s3)&&s2!=s1))//checks to see if two sides are equal, if so it will run and return true if not it will return false
    {
  return true;
    }
    else 
    {
      return false;
    }
  }
  public static boolean isScalene (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is scalene
   Returns boolean result of whether or not the triangle is scalene.*/
  {
    if ((s1!=s2)&&(s1!=s3)&&(s2!=s3))//checks to see if all sides are not equal, if so it will run and return true if not it will return false
    {
      return true;
    }
    else 
    {
      return false;
    }
  }

  public static boolean isAcute (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is acute
   Returns boolean result of whether or not the triangle is acute.*/
  {
    int sqrOne = 0, sqrTwo = 0;
    if (s1<s3&&s2<s3)//checks to see if the lengths of side 1 and 2 are less than side 3
    {
      sqrOne = (s1*s1)+(s2*s2);
      sqrTwo = s3*s3;
 if (sqrOne>sqrTwo)
      {
        return true;
      }
      else 
      {
        return false;
      }
    }
    else if (s1<s2&&s3<s2)//checks to see if the lengths of side 1 and 3 are less than side 2
    {
      sqrOne = (s1*s1)+(s3*s3);
      sqrTwo = s2*s2;
      if (sqrOne>sqrTwo)
      {
        return true;
      }
      else 
      {
        return false;
      }
    }
    else if (s2<s1&&s3<s1)//checks to see if the lengths of side 2 and 3 are less than side 1`
    {
      sqrOne = (s2*s2)+(s3*s3);
      sqrTwo = s1*s1;
      if (sqrOne>sqrTwo)
      {
        return true;
      }
      else 
{
        return false;
      }
    }
    else
    {
      return false;
    }
  }
  public static boolean isObtuse (int s1, int s2, int s3)/*takes inputs: side 1, 2 and 3 as integers. Checks to see if this triangle is obtuse
   Returns boolean result of whether or not the triangle is obtuse.*/
  {
    int sqrOne = 0, sqrTwo = 0;
    if (s1<s3&&s2<s3)//checks to see if the lengths of side 1 and 2 are less than side 3
    {
      sqrOne = (s1*s1)+(s2*s2);
      sqrTwo = s3*s3;
      if (sqrOne<sqrTwo)
      {
        return true;
      }
      else 
      {
        return false;
      }
    }
    else if (s1<s2&&s3<s2)//checks to see if the lengths of side 1 and 3 are less than side 2
    {
      sqrOne = (s1*s1)+(s3*s3);
      sqrTwo = s2*s2;
      if (sqrOne<sqrTwo)
      {
        return true;
      }
      else 
      {
        return false;
      }
    }
    else if (s2<s1&&s3<s1)//checks to see if the lengths of side 2 and 3 are less than side 1
    {
      sqrOne = (s2*s2)+(s3*s3);
      sqrTwo = s1*s1;
      if (sqrOne<sqrTwo)
      {
        return true;
      }
      else 
      {
        return false;
      }
    }
    else
    {
      return false;
    }
  }
}

2 个答案:

答案 0 :(得分:2)

  

在开始时输入3个零,但不在之后输入,

在您第一次有效输入后,您无法获得新值。

当您想要至少询问一次时,通常会很有用。

do {
   side1 = in.nextInt();
   side2 = in.nextInt();
   side3 = in.nextInt();

   if ( side1!=0 || side2!=0 || side3!=0 ) {
      ...
   }
}
while (side1!=0 || side2!=0 || side3!=0);

另外,请注意,您不需要嵌套循环来处理无效数据。如果数据无效,您只需打印一条消息并继续外循环的下一次迭代。

编辑:正如上段所述:

do {
   side1 = in.nextInt();
   side2 = in.nextInt();
   side3 = in.nextInt();

   if ( side1!=0 || side2!=0 || side3!=0 ) {
       if ( side1<=0 || side2<=0 || side3<=0 ) {
          System.out.println("Triangle sides must be positive values.\nPlease enter new values.");
       }
       else if ( side1>=side2+side3 || side2>=side1+side3 || side3>=side2+side1 ) {
          System.out.println("Those sides cannot form a triangle.\nPlease enter new values.");
       }
       else {
           // ... Classify the triangle. 
       }
   }
}
while (side1!=0 || side2!=0 || side3!=0);

答案 1 :(得分:2)

您的代码未终止的原因是嵌套了while循环。虽然您在行中包含了break语句:

if (side1==0&&side2==0&&side3==0)//checks if the user entered 3 zeros to break the loop and terminate the program
    {
      break;
    }

您不会退出外部循环。为了解决这个问题,我建议减少代码中的循环次数,并在组合的if语句中检查有效输入。也就是说,您可以使用单个if语句检查有效输入,而不是在代码中放置两个嵌套的while循环,例如:

if((side1==0&&side2==0&&side3==0) || (side1<0||side2<0||side3<0))

希望这有帮助。