0
点赞
收藏
分享

微信扫一扫

第二周博客

自信的姐姐 2022-03-13 阅读 50

1.小练习
本周信息学院组织了班级篮球赛,现计科1班,计科2班,电气1班,电气2班
各有7,6,8,9名球员,现在需要设计一个程序计算出从他们当中选出5名参赛选手组成新的一支球队代表学院参加比赛,总共有多少种结果

public class Player{
    public static void main(String[] args) {
        int a = 7, b = 6, c = 8, d = 9, i = 0;//四个班各有7,6,8,9名球员
        for (int x = 0; x <= a; x++) {
            for (int y = 0; y <= b; y++) {
                for (int z = 0; z <= c; z++) {
                    for (int m = 0; m <= d; m++)
                    {
                        if (x + y + z + m == 5) {//总共选出五名球员
                            System.out.println("计科1班 " + x + "\t计科2班 " + y + "\t电气1班 " + z +"\t电气2班" + m);
                            i++;
                        }
                }
            }
        }
    }
        System.out.println("有" + i + "结果")//计算出有多少种结果
    }

2.作业中的错题
程序分析题

public class Test01 {
    public static void main(String[] args){
        byte b = 3;
        b = b + 4;
        System.out.println("b="+b);
    }
}

此代码在编译器上无法编译成功,显示为不兼容,正确代码如下

public class Test01 {
    public static void main(String[] args){
        int b = 3;
        b = b + 4;
        System.out.println("b="+b);
    }
}

因为 b = b + 4;中的4的类型默认为int,而不是byte。

public class Test02 {
    public static void main(String[] args){
        int x = 12;
        {
            int y = 96;
            System.out.println("x is" + x);
            System.out.println("y is" + y);
        }
        y = x;
        System.out.println("x is"+x);
    }
}

此代码在编译器上无法编译成功,x不应分配y,正确代码如下

public class Test02 {
    public static void main(String[] args){
        int x = 12;
        int y = 96;
        {

            System.out.println("x is" + x);
            System.out.println("y is" + y);
        }
        System.out.println("x is"+x);
    }
}

此题中的y是定义在第三个{}内,所以不能在第二个{}内将x分配给y。

举报

相关推荐

第二周

假期第二周

第二周buu

第二周总结

第二周作业

DVWA(第二周)

第二周学习

2022第二周周赛题解

0 条评论