Bootstrap

JAVA零基础小白自学日志——第十一天至十四天


今天休息一天,写个小程序玩玩吧

1.设置题目:

[1].随机建立一个行*列,小于等于10*10,大于等于5*5,建立一个二维表;
表一以7*8为例;
表一

XXC0C1C2C3C4C5C6C7
R00001020304050607
R11011121314151617
R22021222324252627
R33031323334353637
R44041424344454647
R55051525354555657
R66061626364656667

[2].从随机产生的二维表单内的数字随机产生三组,三个横纵相邻格内数字组成的连续块;
[3].用户输入表单内的数值,进行猜测,如果猜中显示“Hit”,如果没猜中显示“Miss”,如果猜中一组三个连续数则显示“Kill”,如果三组九个数字全部猜中则显示“Win”,显示猜测次数,统计猜错多少次,显示准确率;
[4].由于没有学习界面的内容,表格只能由+ - |构成;
[5].要求在运行时显示随机输出的二维表,完成猜测后,输出猜中后的表,猜中的数字格内显示为空;

2.逻辑流程

Created with Raphaël 2.3.0 1.开始 2.初始化表格 3.得到用户猜测 4.检查用户猜测 5.是否全部猜对 6.显示猜测结果 7.结束 yes no yes no

3.根据逻辑流程建立逻辑类

Output
Output
«public»
GuessNumbersGame
main(String[] arge)
MatrixOfNumbersOutput
int[] coordinateAxis
setMatrix()
getMatrix()
UserInput
int userInput()
NumerCompare
compare()

4.代码

public class GuessNumbersGame {

	public static void main(String[] args) {
		int[] parameterRegister = new int[9];
		for (int recurOne = 0; recurOne < 9; recurOne++) {
			parameterRegister[recurOne] = 100;
		}
		
		int userVal = 0;
		int sumVal = 0;
		
		MatrixOfNumbersOutput s1 = new MatrixOfNumbersOutput();
		UserInput s2 = new UserInput();
		NumerCompare s3 = new NumerCompare();
		
		s1.setMatrix(parameterRegister);
		
		parameterRegister = s1.getMatrix();
		
		while (sumVal != 9000) {
			
			userVal = s2.userInputValues();		
			s3.valuesParameter(parameterRegister);
			sumVal = s3.compare(userVal);
			
		}//end while
		
		parameterRegister = s1.getMatrix();
		
		System.out.println("您猜测了 "+ UserInput.COUNTER + " 次");
		
		s1.setMatrix(parameterRegister);
		
	}//end main
}//end class

class MatrixOfNumbersOutput{
	
	private static int[] COORDINATE_AXIS = new int[2];//声明行列终值    
	
	static {//给坐标终值赋值
		
		int cdA;
		int cycle = 0;
		while (cycle < 2) {//为coordinateAxis[2]赋值
			
			cdA = (int)(Math.random() * 10+1);//获取一个0~10随机数
			
			if (cdA <= 10 && cdA > 4 ) {//数值范围设定
				
				COORDINATE_AXIS[cycle] = cdA;
				cycle++;
				
			}//end if			
		}//end while
		
		cycle = 0;
		
	}//end static
	void setMatrix(int[] tempParReg) {
		
		int[] matrixParameter = new int[9];
		matrixParameter = tempParReg;
		
		String tempCoordinate;//坐标输出转存
		int valReg = 100;
				
		System.out.println(((matrixParameter[0] == 100) ? "清按照坐标格内的数字选择要猜测的数字:" : "来看看你的成绩吧:"));
		String rowO = "+--+";
		String rowT = "--+";
		
		for (int recurOne = 0; recurOne < 3; recurOne++) {//输出前三行
			
			rowO = (recurOne != 1) ? "+--+" : "|XX|";
			System.out.print(rowO);
			
			for (int recurTwo = 0; recurTwo < COORDINATE_AXIS[1]; recurTwo++) {
				
				rowT = (recurOne != 1) ? "--+" : ("C" + recurTwo + "|");
				System.out.print(rowT);
				
			}//end for recurTwo
			
			System.out.println();
			
		}//end for recurOne
		
		
			
			for (int recurOne = 0; recurOne < COORDINATE_AXIS[0]; recurOne++ ) {
				
				System.out.print("|R" + recurOne + "|");
				
				for (int recurTwo = 0; recurTwo < COORDINATE_AXIS[1]; recurTwo++ ) {
					
					tempCoordinate = (recurOne == 0) ? ("0"+ recurTwo + "|") : (((recurOne*10) + recurTwo) + "|");
					
					for (int tempReg : matrixParameter) {
						
						valReg = tempReg;
						
						if(tempReg == ((recurOne*10) + recurTwo)) break;
					}
					
					tempCoordinate = (valReg == ((recurOne*10) + recurTwo)) ? "KK|" : tempCoordinate;						
					
					
					System.out.print(tempCoordinate);					
					
									
				}//end for recurTwo
				
				System.out.println();
				System.out.print(rowO);
				
				for (int recurThree = 0; recurThree < COORDINATE_AXIS[1]; recurThree++ ) {
					
					System.out.print(rowT);
					
				}//end for recurThree
				
				System.out.println();
				
			}//end for recurOne		
	}//end setMatrix()
	
	int[] getMatrix() {
		
		int[] coordinateValues = new int[9];
		
		for(int recurOne = 0; recurOne < 9; recurOne++) {
			coordinateValues[recurOne] = 100;
		}
		
		int axial = 1;//方向,1横向,10纵向			
		int apex = 0;//顶点坐标
		int[] arrayPo = new int[] {0,0,0};//数组指针
		int[] apexR = new int[] {100,100,100,100};
		boolean[] judgement = new boolean[] {false,false,false};		
		
		for(int recurOne = 0; recurOne < 3; recurOne++) {
			
			while (!judgement[0] && !judgement[1]) {				
					
				axial = (int)(Math.random() * 10) >4 ? 10 : 1;
				apex = (int)(Math.random()*100);
				
				judgement[0] = (axial == 10) ? ((apex/10%10)+2 < COORDINATE_AXIS[0] && (apex%10) < COORDINATE_AXIS[1]) : ((apex/10%10) < COORDINATE_AXIS[0] && (apex%10)+2 < COORDINATE_AXIS[1]);
							   
			    if (judgement[0]) {
			    	
			    	while (arrayPo[0] < 3) {
			    		
			    		apexR[0] = apex + (arrayPo[0] * axial);
			    		
			    		for(int tempReg : coordinateValues) {
			    			
			    			apexR[3] = tempReg;
			    			
			    			if (apexR[0] == tempReg) break;
			    		
			    		}
			    		
			    		if (apexR[0] == apexR[3]) {
			    			
			    			arrayPo[0] = 3;
			    			apexR[0] = 100;
			    			apexR[3] = 100;
			    			judgement[0] = false;
			    			judgement[1] = false;
			    			
			    		}else {
			    			
			    			arrayPo[0]++;
			    			apexR[0] = 100;
			    			apexR[3] = 100;
			    			judgement[1] = true;
			    		
			    		}			    		
			    	}//end while (arrayPo[0] < 3)
			    	
			    	arrayPo[0] = 0;
			    
			    }else {
			    	
			    	judgement[1] = false;
			    
			    }
			        	
			    	
			}//end while judgementO && judgementT
			
			for(int recurTwo = 0; recurTwo < 3; recurTwo++) {
				
				coordinateValues [arrayPo[1]] = apex + (recurTwo * axial);
				arrayPo[1]++;
				
			}//end for recurTwo	
			
			judgement[0] = false;
			judgement[1] = false;
		
		}//end for recurOne
		//for (int i = 0; i < 9; i++) {
			//System.out.println(coordinateValues[i]);
		//}
		
		return coordinateValues;
	}//end getMatrix()
	
}//end class MatrixOfNumbersOutput

class UserInput{
	
	static int COUNTER;
	
	int userInputValues () {
		
		System.out.print("请输入你猜测的坐标并按回车结束: ");
		java.util.Scanner userInput = new java.util.Scanner(System.in);		
		int inputValues = userInput.nextInt();
		//userInput.close();
		COUNTER++;
		return inputValues;
	}
	
}//end class UserInput

class NumerCompare{
	
	private static int[] PARAMETER = new int[9];
	
	void valuesParameter(int[] tempParReg) {
		
		PARAMETER = tempParReg;
		//for(int i = 0; i <9;i++) {
			//System.out.println(PARAMETER[i]);
		//}
	}//end valuesParameter()
	
	int compare(int tempUserVal) {
		int vlanesU = tempUserVal;
		int userVlanReg = 100;
		int topV = 100;
		int sum = 0;
		String gameResult = "Miss";
		for (int tempV = 0; tempV < 9; tempV++) {
			if (vlanesU == PARAMETER[tempV]) {
				topV = tempV;
				PARAMETER[tempV] = 1000;
				gameResult = "Hit";
				break;
			}//end if
		}//end for tempV
		if (topV < 9 ) {
		    if (PARAMETER[topV] == 1000) {
			
			   if(topV >= 0 && topV < 3) {
				   
				   userVlanReg = 0;
				   
			   }else if(topV >= 3 && topV < 6) {
				   
				   userVlanReg = 1;
				   
			   }else if(topV >= 6 && topV < 9) {
				   
				   userVlanReg = 2;
				   
			   }//end if topV0~9
			   
			   switch (userVlanReg) {
			   
			   case 0:
				   
				   gameResult = ((PARAMETER[0] + PARAMETER[1] +PARAMETER[2]) == 3000) ? "Kill" :gameResult;
				   break;
				   
			   case 1:
				   
				   gameResult = ((PARAMETER[3] + PARAMETER[4] +PARAMETER[5]) == 3000) ? "Kill" :gameResult;
				   break;
				   
			   case 2:
				   
				   gameResult = ((PARAMETER[6] + PARAMETER[7] +PARAMETER[8]) == 3000) ? "Kill" :gameResult;
				   break;
				   
			   default:
				   
				   gameResult = "Miss";
			       topV = 100;
			       userVlanReg = 100;
				   break;
				   
			   }//end switch
		    }else {
		    	
		    	gameResult = "Miss";
		    	topV = 100;
		    	userVlanReg = 100;
		    	
		    }//end if PARAMETER[topV] == 1000 else
		}else {
			
			gameResult = "Miss";
			topV = 100;
			userVlanReg  = 100;
			
		}//end if top<9 else
		
		for (int counOne = 0; counOne < 9; counOne++) {
			
			sum = PARAMETER[counOne] + sum;
			
		}
		
		if (sum == 9000) {
			
			gameResult = "Win";
			
		}
		
		System.out.println(gameResult);
		return sum;
	}
	
}//end class NumerCompare

这么一段代码写了这么多天也是没想到的,一直在跟各种变量初始化斗争,看来还是练习的少,下一篇日志再来分析,这一篇的错误和疏漏吧,然后看看代码可以如何优化,应该说是很笨拙的一段代码了。

;