ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java练习题5

2021-04-13 22:29:11  阅读:102  来源: 互联网

标签:练习题 Java String int System shape public out


第1题.

public class MobilPhone {
    public String brand;
    public MobilPhone(){
        this.brand="诺基亚";
    }
    public MobilPhone(String bra){
        this.brand=bra;
    }
    public String buy(){
        return"没发工资, 买一个"+brand+"牌子的手机吧!";
    }
    public String buy(String reason){
        return reason+", 快买一个"+brand+"牌子的手机吧!";
    }
}

测试代码 :

public class MobilPhoneTest {
    public static void main(String[] args) {
        MobilPhone mp = new MobilPhone();
        mp.brand="苹果";
        String detail=mp.buy("发工资啦");
        System.out.println(detail);
    }
}

运行结果

发工资啦, 快买一个苹果牌子的手机吧!

进程已结束,退出代码为 0

第2题

public class One {
    static int year;
    static double a;
    static double b;
    static double c;
    public double i(double i,int yearchice){
        if (yearchice==1){
            a=0.0603;
            year=36;
            c=i*a;
        }else if (yearchice==2){
            a=0.0612;
            year=60;
            c=i*a;
        }else if (yearchice==3){
            a=0.0639;
            year=240;
            c=i*a;
        }
        b=(i+c)/year;
        System.out.println("***月供为:"+b);
        return b;
    }
}

测试代码 :

public class Two {
    public static void main(String[] args) {
        One money=new One();
        Scanner input=new Scanner(System.in);
        System.out.println("请输入贷款金额:");
        double i=input.nextDouble();
        System.out.print("\n请选择贷款年限:1.3年(36个月)\t2.5年(60个月)\t3.20年(240个月):");
        int n= input.nextInt();
        money.i(i,n);
    }
}

 

运行结果 :

请输入贷款金额:
100000

请选择贷款年限:1.3年(36个月)	2.5年(60个月)	3.20年(240个月):3
***月供为:443.2916666666667

进程已结束,退出代码为 0

第3题.

public class Triangle {
    public boolean isTriangle(int a,int b,int c){
        boolean flag=false;
        if ((a + b > c) && (a + c > b) && (b + c > a)){
            flag=true;
        }
        return flag;
    }
    public String shape(int a, int b, int c){
        String shape="";
        if (a+b<=c||a+c<=b||b+c<=a){
            System.out.println("这不能构成三角形。");
        }else if(a==b&&b==c){
            shape="等边三角形";
            System.out.println("这是一个"+shape);
        } else if (a*a==b*b+c*c || b*b==a*a+c*c || c*c==a*a+b*b) {
            shape="直角三角形";
            System.out.println("这是一个" +shape);
        }else if(a*a<b*b+c*c || b*b<a*a+c*c || c*c<a*a+b*b){
            shape="锐角三角形";
            System.out.println("这是一个" +shape);
        }
        else {
            shape="钝角三角形";
            System.out.println("这是一个" +shape );
        }
        return shape;

    }
}

测试代码 :

public class Test {
    public static void main(String[] args) {
        boolean i=true;
        Triangle t = new Triangle();
        String m="";
        Scanner input= new Scanner(System.in);
        do {
            System.out.print("请输入第一个边:");
            int a = input.nextInt();
            System.out.print("请输入第二个边:");
            int b = input.nextInt();
            System.out.print("请输入第三个边:");
            int c = input.nextInt();
            t.isTriangle(a, b, c);
            t.shape(a, b, c);
            System.out.print("继续吗?(y/n)");
            m= input.next();
            if (m.equals("n")){
                System.out.println("谢谢使用!");
                break;
            }
        }while (m.equals("y"));
    }

}

运行结果:

请输入第一个边:1
请输入第二个边:1
请输入第三个边:3
这不能构成三角形。
继续吗?(y/n)y
请输入第一个边:6
请输入第二个边:6
请输入第三个边:6
这是一个等边三角形
继续吗?(y/n)y
请输入第一个边:6
请输入第二个边:4
请输入第三个边:3
这是一个锐角三角形
继续吗?(y/n)n
谢谢使用!

进程已结束,退出代码为 0

标签:练习题,Java,String,int,System,shape,public,out
来源: https://blog.csdn.net/weixin_51345562/article/details/115680725

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有