⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ballselector.java

📁 用面向对象思想完成的9个重要外观的球
💻 JAVA
字号:
class ball   //this represent each ball that wait to be selected
{
	int pos;     //pos represent the position of the ball
	float weight;
	void ball()
	{
	}
	void speak()
	{
		System.out.println("恭喜恭喜,您终于找到了,我就是那个特殊的球");
		System.out.println("我是第 "+ pos + "个球,我的重量是 " + weight);
	}
}

class weighter  
{
	int compareresult;//0 means equal result; -1 means the first ball is lighter; 1 means the 2nd one is lighter;
	void weighter()
	{
	}
	
	void beginweight(ball bb1,ball bb2)
	{
		if (bb1.weight > bb2.weight)
		  compareresult=1;//the first ball is heavier
		else
		{
		  if (bb1.weight < bb2.weight)
		  	compareresult = -1;//the first ball is lighter;
		  else 
		    compareresult = 0;//the 2 balls weight equal;
		}
	}
}

class bigball
{
	ball[] sb=new ball[4];
	void bigball()
	{
	  for (int i=0;i<4;i++)
	    sb[i]=new ball();
	}
	
	void combineball()
	{
		sb[3].weight=sb[1].weight+sb[2].weight+sb[0].weight;
	}
}

public class ballselector  //find the different one between the 3 ones.
{
	int heavier;       //if 1 means true, -1 means false, 0 means unknown;
	int pos, findnum;  //findnum represent the time that you weight the balls.
	void finddifferentball()
	{
		pos=0;
		findnum=0;
		heavier=0;
	}
	
	int findthedifferentone(ball b1, ball b2, ball b3,weighter w)
	{
		w.beginweight(b1,b2);
		findnum++;
		if (w.compareresult==0)//the first 2 balls weight equal,so the 3rd ball is the different one.
			pos=3;   
		else  //the 2 balls include the different one,so the 3rd one is normal.
		{
		  if (findnum ==1)
		  {  
 	      heavier=w.compareresult*(-1);
		 	  w.beginweight(b1,b3);
		    findnum++;
		    if (w.compareresult==0) //the first one is heavier
		     	pos=2;		//the 1st one is the different one.
		    else
		    { 
	        pos=1;
		      heavier=w.compareresult;
		    }
		  }
		  else
		  {
		    if (heavier==0)
		   	{	
		   		heavier=w.compareresult;
			    w.beginweight(b1,b3);//continue to find whethghter the 3rd ball is lighter.
      	  findnum++;
			    if (w.compareresult==0)
			    {
			      heavier=heavier*(-1);
			      pos=2;
			    }
			    else
			      pos=1;
        }
		    else if (heavier==1)//the heavier one is the different.
		    {
		      if (w.compareresult==1)//the 1st is heavier
		        pos=1;
		      else
		        pos=2;
		    }
		    else
		    {
		      if (w.compareresult==1)//the 1st is heavier
		        pos=2;
		      else
		        pos=1;
		    }
		  }
		}
		return pos;
	}
}

class A
{
	public static void main(String args[])
	{
		if (args.length!=9)
		{
			System.out.println("您输入的参数错误!使用方法为:");
			System.out.println("java A b1 b2 b3 b4 b5 b6 b7 b8 b9");
			System.out.println("b1到b9代表9个球的重量");
			return ;
		}
		ball[] b=new ball[9];   //this represent the 9 balls that wait to select
		bigball grp[]=new bigball[3];//this represent the 3 groups that contain the balls.
		ball[] big=new ball[3]; //this represent the new 3 big balls for weight.
		int i,j,k=0;
		ballselector fdb=new ballselector();//fdb select the different one.
		fdb.findnum=0;
		weighter w=new weighter(); //this represent the weighter.
		w.compareresult=-2;
		for (i=0;i<3;i++)   //get the weight of the 9 balls
		{
			grp[i]=new bigball();   //initialize each of the group
      big[i]=new ball();      //at the same time initialize the big ball.
      big[i].pos=0;
      big[i].weight=0;
			for (j=0;j<3;j++)
			{
				k=i*3+j;              //k represent the position of each true ball.
				b[k]=new ball();      //initialize each of the small ball.
        b[k].pos=k+1;       //k+1 is the true position
        b[k].weight=Float.parseFloat(args[k]); //从键盘(系统标准输入)获取字符
        grp[i].sb[j]=b[k];  //send the balls to the definite group.
      }
      grp[i].sb[j]=big[i];
      grp[i].combineball();
	  }
	  
	  i=fdb.findthedifferentone(grp[0].sb[3],grp[1].sb[3],grp[2].sb[3],w)-1;
	  j=fdb.findthedifferentone(grp[i].sb[0],grp[i].sb[1],grp[i].sb[2],w)-1;
	  grp[i].sb[j].speak();
	  System.out.println("总共进行了"+fdb.findnum+"次称量");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -