簡體   English   中英

使用Java分析三角形的類型

[英]Analyzing the type of triangle using Java

下面的代碼應該根據用戶在終端中輸入的角度來分析三角形的類型。 但是,在第一個if語句中,盡管我將其設置為(a+b+c==d) -仍然在終端中,如果我輸入180,1,1-而不是停止執行並打印Triangle is not possible. ,而是說三角形是等腰。 我肯定是一個錯誤。 但是我是菜鳥,所以請更正我的說法。

/**
 * @date 20/4/2014
 */ 


import java.io.*;
public class Triangleanglederivation
{
       public static void main (String args[])throws IOException
    {
        InputStreamReader read = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(read);
        int a,b,c,d; 
        d=180;
        System.out.println("Enter the three sides of the triangle:");
        a=Integer.parseInt(in.readLine());
        b=Integer.parseInt(in.readLine());
        c=Integer.parseInt(in.readLine());        
        if(a+b+c==d)
            System.out.println("Triangle is possible.");    
        if((a==b)&&(b==c)&&(c==a))
            System.out.println("The triangle is equilateral.");
        else if((a==b)||(b==c)||(c==a))
            System.out.println("The triangle is isosceles.");
        else if((a!=b)&&(b!=c)&&(c!=a))
            System.out.println("The triangle is scalene.");
        else
            System.out.println("Triangle is not possible.");
    }
}

固定

if((a==b)&&(b==c)&&(c==a)&&(a+b+c==d))
  System.out.println("The triangle is equilateral.");
else if(((a==b)||(b==c)||(c==a))&&(a+b+c==d))
  System.out.println("The triangle is isosceles.");
else if((a!=b)&&(b!=c)&&(c!=a)&&(a+b+c==d))
  System.out.println("The triangle is scalene.");
else
  System.out.println("Triangle is not possible.");

無論三角形是否可行,都將檢查三角形是否等邊,等腰和斜角。 您需要向使用&&每個對象添加可能性檢查(a+b+c==d)或嵌套if / else,以使它們僅在if (a+b+c==d)成功后才發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM