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

📄 guess.jsp

📁 《JSP时尚编程百例》一例源代码21-40
💻 JSP
字号:
<%!
public String getMessage( int[] result )
{
	switch( result[0] )
	{
   	case -3: return "请输入完整的4位数";
		case -2: return "输入的格式有错误";
   	case -1: return "有重复的数字";
  	case  0: return "<font color=red size=4 align=center>聪明伶俐100分!恭喜您答对了!</center>" ;
  	default: return result[1]+"A "+result[2]+"B"+"  请继续努力";
  }
}
public int[] checkNumber( String guessNum, String answerNum )
{
	if( guessNum.length() != 4 )
	return new int[] { -3, 0, 0 } ;
	int num=0 ;
  try
  {
  	num=Integer.parseInt( guessNum ) ;
  }
  catch( NumberFormatException ex )
  {
		return new int[] { -2, 0, 0 } ; 
  }
  int[] timesDigit={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ;
  for( int i=0 ; i<4 ; i++ )
  {
  	int ptr=num%10 ;
   	num=num/10 ;
		if( ++timesDigit[ptr]>1 )
			return new int[] { -1, 0, 0 } ;
  }
  int a=0 ;
  int b=0 ;
  for( int j=0 ; j<4 ; j++ )
  {
  	int pos=answerNum.indexOf( guessNum.substring( j, j+1 ) ) ;
  	if( pos==j )
  		a++ ;
  	else if( pos>=0 )
   		b++ ;
  }
  if( a==4 )
  	return new int[] { 0, a, b } ;
  else
  	return new int[] { 1, a, b } ;
}
public String randNum()
{
	int[] digits={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
  Random rnum=new Random( ( new Date() ).getTime() ) ;
  for( int i=0 ; i<digits.length ; i++ )
  {
  int ptr=Math.abs( rnum.nextInt() )%10 ;
  int tmp=digits[ptr] ;
  digits[ptr]=digits[i] ;
  digits[i]=tmp ;
  }
  return Integer.toString( digits[0] )+Integer.toString( digits[1] )
         +Integer.toString( digits[2] )+Integer.toString( digits[3] ) ;
}
%>

⌨️ 快捷键说明

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