按照first head java 所说的 从今天开始我才正式进入对象村,才开始面向对象的学习,进行面向对象的相互调用
这是一个猜数字的小程序
import java.io.*; import java.util.*; import java.math.*; class Player //这里class前面貌似不能加public 不知道为什么 { int number; public void count (){ number=(int)(Math.random()*10); System.out.println("this number is "+number); } } class GuessGame //这里class前面貌似不能加public 不知道为什么 { Player a=new Player(); Player b=new Player(); Player c=new Player(); public void startGame() { int x,y,z; int number; boolean judgea=false,judgeb=false,judgec=false; number=(int) (Math.random()*10); System.out.println("please put the number between 0-9"); System.out.println("the orignal number is "+number); while(true) { a.count(); b.count(); c.count(); x=a.number; y=b.number; z=c.number; if(x==number)judgea=true; if(y==number)judgeb=true; if(z==number)judgec=true; if(judgea||judgeb||judgec) { if(judgea==true)System.out.println("player one is correct he get "+x); else System.out.println("player one is wrong he get "+x); if(judgeb==true)System.out.println("player two is correct he get "+y); else System.out.println("player two is wrong he get "+y); if(judgec==true)System.out.println("player three is correct he get "+z); else System.out.println("player three is wrong he get "+z); break; } else System.out.println("Please try again!!!"); } } } public class GameLanuch { public static void main(String args[]){ GuessGame a=new GuessGame(); a.startGame(); } }