.有红、黄、黑、白4色球各一个,放置在编号为1,2,3,4的4个盒子里面,每个盒子一个球,顺序未知。甲、乙、丙三个人猜测放置顺序如下:甲:黑球在1号盒子,黄球在2号盒子;乙:黑球在2号盒子,白球在3号盒子;丙:红球在2号盒子,白球在4号盒子;结果证明:甲、乙、丙三人各猜中一半,试通过程序求出4色球在盒子中的放置位置。
public class java3_3 {public static void main(String[] args) { // TODO Auto-generated method stub String a="1"; String b="1"; String c="1"; String d="1"; for(int i=1;i<=2;i++) { if(i==1) a="black"; if(i==2) b="yellow"; for(int j=1;j<=2;j++) { if(j==1) b="black"; if(j==2) c="white"; for(int k=1;k<=2;k++) { if(k==1) b="red"; if(k==2) d="white"; if(a!=b&&b!=c&&c!=d&&d!=a) { if(a!="1") System.out.println("1号盒子的颜色是:"+a); else { if(a!="black"&&a!="red"&&a!="white") a="yellow"; else if(a!="yellow"&&a!="red"&&a!="white") a="black"; else if(a!="yellow"&&a!="black"&&a!="white") a="red"; else a="white"; System.out.println("1号盒子的颜色是:"+a); } if(b!="1") System.out.println("2号盒子的颜色是:"+b); else { if(b!="black"&&b!="red"&&b!="white") c="yellow"; else if(b!="yellow"&&b!="red"&&b!="white") b="black"; else if(b!="yellow"&&b!="black"&&b!="white") b="red"; else b="white"; System.out.println("2号盒子的颜色是:"+b); } if(c!="1") System.out.println("3号盒子的颜色是:"+c); else { if(c!="black"&&c!="red"&&c!="white") c="yellow"; else if(c!="yellow"&&c!="red"&&c!="white") c="black"; else if(c!="yellow"&&c!="black"&&c!="white") c="red"; else c="white"; System.out.println("3号盒子的颜色是:"+c); } if(d!="1") System.out.println("4号盒子的颜色是:"+d); else { if(d!="black"&&d!="red"&&d!="white") c="yellow"; else if(d!="yellow"&&d!="red"&&d!="white") d="black"; else if(d!="yellow"&&d!="black"&&d!="white") d="red"; else d="white"; System.out.println("4号盒子的颜色是:"+d); } } } } } } }