第二周博客

时间: 2023-07-18 admin 互联网

第二周博客

第二周博客

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。