Java While语句正在检查但允许无效输入

Java While语句正在检查但允许无效输入,java,math,while-loop,geometry,error-checking,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 wi

我正试图写一个程序,把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;
    }
  }
}
*程序说明:
*此程序仅使用整数值读取任意数量的三角形边集。该方案将:
*提示用户输入数字集并进行处理,直到用户提交数字0,这将终止程序。
*对于每组三个数字,程序将打印读取的值。
*对于每一组三个数字,程序将决定这些数字是否代表有效三角形的边。
*如果数字不能表示有效的三角形,它将显示相应的错误消息。
*如果数字有效,程序将确定并显示:
*三角形的侧面分类–等边、等腰或不等边
*三角形的角度分类-直角、锐角或钝角*/
导入java.util.Scanner;
类三角形类型
{
公共静态void main(字符串[]args)
{
intside1=-6,side2=-1,side3=-1;
扫描仪输入=新扫描仪(系统输入);
System.out.println(“请输入代表三角形有效边的三个整数。输入0以终止程序。”);
side1=in.nextInt();
side2=in.nextInt();
side3=in.nextInt();
while(side1!=0&&side2!=0&&side3!=0)//检查用户是否输入了3个零以跳过循环并终止程序
{
while((side1=side2+side1))//检查用户输入的side值是否有效
{
System.out.println(“您至少输入了一个无效值。这意味着您的值不能构成三角形。请输入新值。”);
side1=in.nextInt();
side2=in.nextInt();
side3=in.nextInt();
if(side1==0&&side2==0&&side3==0)//检查用户是否输入了3个零以中断循环并终止程序
{
打破
}
}
}
if(side1==0&&side2==0&&side3==0)//检查用户是否输入了3个零来终止程序,否则程序将运行
{
System.out.println(“您已选择终止程序。”);
}
其他的
{
System.out.println(“您输入的边长有效:“+side1+”、“+side2+”和“+side3”);
if((isIsosceles(第1、2、3边))==true)
{
System.out.println(“三角形的侧面分类:等腰”);
}
else if((isEquilateral(side1,side2,side3))==true)
{
System.out.println(“三角形的侧面分类:等边”);
}
其他的
{
System.out.println(“三角形的侧面分类:不等边”);
}
如果((isAcute(side1、side2、side3))==true)
{
System.out.println(“三角形的角度分类:锐角”);
}
其他的
{
System.out.println(“三角形的角度分类:钝角”);
}
}
}
公共静态布尔isEquilateral(ints1、ints2、ints3)/*将输入:边1、2和3作为整数。检查此三角形是否为等边三角形
返回三角形是否等边的布尔结果*/
{
if((s1==s2)&&(s2==s3))//检查所有边是否相等,如果相等,它将运行并返回true,如果不相等,它将返回false
{
返回true;
}
其他的
{
返回false;
}
}
公共静态布尔isIsosceles(int s1,int s2,int s3)/*将输入:边1,2和3作为整数。检查此三角形是否为等腰三角形
返回三角形是否为等腰三角形的布尔结果*/
{
如果((s1==s2)和&s1!=s3)| |((s1==s3)和&s1!=s2)| |((s2==s3)和&s2!=s1))//检查两边是否相等,如果相等,则运行并返回true,如果不相等,则返回false
{
返回true;
}
其他的
{
返回false;
}
}
公共静态布尔isScalene(ints1,ints2,ints3)/*接受输入:边1,边2和边3作为整数。检查此三角形是否为不定数
返回三角形是否为不等边三角形的布尔结果*/
{
if((s1!=s2)&&&(s1!=s3)&&&(s2!=s3))//检查所有边是否不相等,如果不相等,它将运行并返回true,如果不相等,它将返回false
{
返回true;
}
其他的
{
返回false;
}
}
公共静态布尔值isAcute(ints1,ints2,ints3)/*接受输入:边1,边2和边3作为整数。检查此三角形是否锐角
返回三角形是否锐角的布尔结果*/
{
int sqrOne=0,sqrTwo=0;
如果(s1
在开始时输入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);
do{
side1=in.nextInt();
side2=in.nextInt();
side3=in.nextInt();
如果(边1!=0 | |边2!=0 | |边3!=0){
如果(侧1=侧2+侧1){
System.out.println(“这些边不能形成三角形。\n请输入新值”);
}
否则{
//…对三角形进行分类。
}
}
}
而(边1!=0 | |边2!=0 | |边3!=0);

代码未终止的原因是嵌套了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;
    }
您将不会退出外部循环。为了解决这个问题,我建议减少代码和chec中的循环数
if((side1==0&&side2==0&&side3==0) || (side1<0||side2<0||side3<0))