2021-05-07

今日练习题:

1.请写出以下程序运行结果:
class Main extends Thread {
Sub c;
public Main(Sub calc) {
c = calc;
}
public void run() {
synchronized© {
try {
System.out.println(“Waiting for calculation…”);
c.wait();
} catch (InterruptedException e) {}
}
System.out.println("Total is: " + c.total);
}
public static void main(String [] args) {
Sub calculator = new Sub();
calculator.start();
new Main(calculator).start();
new Main(calculator).start();
new Main(calculator).start();
}
}
class Sub extends Thread {
int total;
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
}
synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
notifyAll();
}}}
结果:
Waiting for calculation…
Waiting for calculation…
Waiting for calculation…
Total is: 4950
Total is: 4950
Total is: 4950

2.请写出以下程序运行结果:

public class MyFor{
public static void main(String argv[]){
int i, j;
outer: for (i=1;i ❤️;i++)
inner: for (j=1; j<3; j++) {
if (j==2)continue outer;
System.out.println(“Value for i=” + i + " Value for j=" +j);
}
}
}
结果:
Value for i=1 Value for j=1
Value for i=2 Value for j=1

3.请写出以下程序运行结果:

public class Note {
public static void main(String args[]) {
String name[] = {“Killer”,“Miller”};
String name0 = “Killer”;
String name1 = “Miller”;
swap(name0,name1);
System.out.println(name0 + “,” + name1);
swap(name);
System.out.println(name[0] + “,” + name[1]);
}
public static void swap(String name[]) {
String temp;
temp=name[0];
name[0]=name[1];
name[1]=temp;
}
public static void swap(String name0,String name1) {
String temp;
temp=name0;
name0=name1;
name1=temp;
}
}
结果:
Killer,Miller
Miller,Killer

穷且益坚,不坠青云之志。——王勃

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值