2014-11-14 85 views
-1

我正在編寫一個程序,讀取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; 
    } 
    } 
} 
+1

請正確說明您的問題!我不明白你在問什麼。另外請注意,「詢問調試幫助」的問題需要包含一個「最小完整可驗證的示例」(又名。[MCVE](http://stackoverflow.com/help/mcve))以作爲主題。 – Vogel612 2014-11-15 17:26:54

回答

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

但是如果輸入1個或多個無效的輸入,它會再次詢問並且在輸入3個零後不會終止 – 2014-11-14 16:50:09

+0

在輸入有效的輸入之前,您還有其他輸入。在循環頂部有一組輸入語句會使這更容易。 – 2014-11-14 16:53:39

+0

你能看看我對ZakDaniels的評論答案,你的聲望看起來像你可能會幫助;) – 2014-11-15 03:50:27

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語句如:

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

希望這有助於。

+0

雖然嵌套的while循環是不必要的複雜,但它們不是代碼沒有終止的原因。如果需要相同的錯誤消息,將錯誤條件組合成單個if語句是合理的,但是也不解決未終止的問題。 – 2014-11-14 18:34:27

+0

@ ZakDaniels99這肯定有幫助,但是,它並沒有消除它允許輸入如0,0,1或0,-1,2 ......的事實當我看它時,它應該只接受零,如果它是全部進入3次。現在也接受像3,7,1這樣的值,但不能形成三角形 – 2014-11-15 03:25:11