java题目这样做为什么三角形部分无法构成三角型时会死循环 2C
package student;
import java.util.Scanner;
public class mianji {
public static void main(String[] args){
    System.out.println("形状的类型\n1.长方形\n2.圆形\n3.三角形\n4.退出");
    Scanner scan=new Scanner(System.in);
    int a=scan.nextInt();
    if(a<=4&&a>=1){
        switch(a){
        case 1:
            System.out.println("Please enter length");
            float b=scan.nextFloat();
            System.out.println("Please enter highth");
            float c=scan.nextFloat();
            if(b>=0&&c>=0){
                System.out.println("Area of the rectangle is:"+(c*b));
            }else{System.out.println("b and c can't be zero");}
        case 2:
            System.out.println("Please enter redius");
            float d=scan.nextFloat();
            if(d>=0){
                System.out.println("Area of the cylnder is:"+(d*d*Math.PI));
            }else{System.out.println("d can't be zero");}
        case 3:
            System.out.println("Plese enter A");
            float A=scan.nextFloat();
            System.out.println("Plese enter B");
            float B=scan.nextFloat();
            System.out.println("Plese enter C");
            float C=scan.nextFloat();
            if(A>=0&&B>=0&&C>=0){
                while(true){if(A+B>C){
                float p=(A+B+C)/2;
                float sum=(float) Math.sqrt((p*(p-A)*(p-B)*(p-C)));
                System.out.println("The triangle area is "+sum);
                }
                else{
                    System.out.println("Three sides can not constitute a triangle");
                }
            }}else{
                System.out.println("A,B,C can't be zero");
            }
        case 4:
            System.out.println("Bye Bye");
            }

        }
    else{
        System.out.println("Please enter 1~4 number");
    }
}

}

三角形部分是case 3 ;然后要怎么弄才能可以计算完条回到选择形状的界面继续算

5个回答

public static void main(String[] args) {

System.out.println("形状的类型\n1.长方形\n2.圆形\n3.三角形\n4.退出");
Scanner scan=new Scanner(System.in);
boolean flag = true;
while(scan.hasNextInt()&&flag==true){
int a=scan.nextInt();
if(a<=4&&a>=1){
    switch(a){
    case 1:
        System.out.println("Please enter length");
        float b=scan.nextFloat();
        System.out.println("Please enter highth");
        float c=scan.nextFloat();
        if(b>=0&&c>=0){
            System.out.println("Area of the rectangle is:"+(c*b));
        }else{System.out.println("b and c can't be zero");}
        break;
    case 2:
        System.out.println("Please enter redius");
        float d=scan.nextFloat();
        if(d>=0){
            System.out.println("Area of the cylnder is:"+(d*d*Math.PI));
        }else{System.out.println("d can't be zero");}
        break;
    case 3:
        System.out.println("Plese enter A");
        float A=scan.nextFloat();
        System.out.println("Plese enter B");
        float B=scan.nextFloat();
        System.out.println("Plese enter C");
        float C=scan.nextFloat();
        if(A>=0&&B>=0&&C>=0){
            if(A+B>C&&B+C>A&&A+C>B){
            float p=(A+B+C)/2;
            float sum=(float) Math.sqrt((p*(p-A)*(p-B)*(p-C)));
            System.out.println("The triangle area is "+sum);
            } else{
                System.out.println("Three sides can not constitute a triangle");
            }
        }else{
            System.out.println("A,B,C can't be zero");
        }
        break;
    case 4:
        System.out.println("Bye Bye");
        flag=false;
        break;
        }

    }
else{
    System.out.println("Please enter 1~4 number");
}

}
}


u014034934
逗比123号 就是判断是不是还有下一个int类型的数据,有就一直执行while里面的代码
接近 3 年之前 回复
m0_37226772
MineralTea 请问这段话是什么意思
接近 3 年之前 回复
m0_37226772
MineralTea boolean flag = true; while(scan.hasNextInt()&&flag==true)
接近 3 年之前 回复

我记得swtich是要加break的

case 3里的while(true ) =死循环,里面的逻辑函数完成后应该有break跳出循环

switch里面是要加break的吧

问题1、switch中每个case都要有对应的break;
switch(id){
case 0x01:
funcA();
break;

case 0x02:
funcB();
break;
}
问题2、
if(A>=0&&B>=0&&C>=0){
while(true){if(A+B>C){
float p=(A+B+C)/2;
}
}
}
逻辑有误,只要满足A B C大于0就会进入while(true){}死循环,当不满足三角形边的要求就没有出口break或者return

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!