问题是:三个人频繁通过只允许一个人经过的门,当人们通过门时,统计人数另外还会记录通行者的姓名和出生地,当姓名和出生地的头一个字母不相等时输出这是第几人通过,姓名和出生地在哪门:public class Gate {
private int count=0;
private String Pname="Nobody";
private String Paddress="Nowhere";
public void pass(String Pname,String Paddress){
this.count++;
this.Pname=Pname;
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
this.Paddress=Paddress;
if(Pname.charAt(0)!=Paddress.charAt(0)){
System.out.println("*******broken********"+toString());}
}
public String toString(){
return "No."+count+":"+Pname+","+Paddress;
}
}通过的人:public class User extends Thread{
private final String name;
private final String address;
private final Gate gate;
public User(Gate gate,String name,String address) {
this.name=name;
this.address=address;
this.gate=gate;
}
public void run(){
System.out.println(name+" begin");
while(true){
gate.pass(name, address);
}
}
}主函数:public class Main {
public static void main(String[] args) {
Gate gate=new Gate();
new User(gate, "Alice", "Aust").start();
new User(gate, "Bob", "Brazil").start();
new User(gate, "Chris", "Canada").start();
}
}按理说应该是很快就能得出经过的人姓名和出生地的头一个字母会不相等,但就是出不来,当把下面不等号改成等号反而会得出结果,不明白这是为什么if(Pname.charAt(0)!=Paddress.charAt(0))没有C币,大家见谅!!!在此小的谢谢您了!
3 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
第一看你参数
public class Main { public static void main(String[] args) { Gate gate=new Gate(); new User(gate, "Alice", "Aust").start(); new User(gate, "Bob", "Brazil").start(); new User(gate, "Chris", "Canada").start(); } }
第二看这个方法
private int count=0; private String Pname="Nobody"; private String Paddress="Nowhere"; public void pass(String Pname,String Paddress){ this.count++; this.Pname=Pname; try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } this.Paddress=Paddress; if(Pname.charAt(0)!=Paddress.charAt(0)){ System.out.println("*******broken********"+toString());} }
该说的都说了,望采纳
添加回答
举报
0/150
提交
取消