0
点赞
收藏
分享

微信扫一扫

java实验(1)

sullay 2022-03-24 阅读 57
java

实验1

(1)猜数字

编写一个Java应用程序,实现如下功能:
A. 随机分配给客户一个1~100之间的整数。
B. 用户从键盘输入自己的猜想。
C.程序返回提示:”大了”,”小了”,”猜对了”。
D. 用户根据提示,再次输入猜想的数,直到猜对为止,显示”猜对了”,同时输出猜想的次数。

package Guess;
import java.util.Random;
import java.util.Scanner;
public class Guess {
     
	public static void main(String[] args)
	{
		Random r = new Random();
		int ran1 = r.nextInt(100);
		Scanner reader = new Scanner(System.in);
		int ant = 0;
		while(true)
		{
			shows();
			double ans = reader.nextDouble();
			if(ans == (double)ran1) 
			{
				ant++;
				System.out.println("猜对了");
				System.out.println("游戏结束了");
				System.out.println("共猜了"+ ant + "次" );
				break;
			}
			else if(ans > (double)ran1)
			{
				ant++;
				System.out.println("大了");
				System.out.println("游戏继续");
			}
			else 
			{
				ant++;
				System.out.println("小了");
				System.out.println("游戏继续");
			}
			
		}
		reader.close();
	}
	public static void shows()
	{
		System.out.println("-----欢迎来到数字邮箱-------");
		System.out.println("------请输入一个数字--------");
	}
     
}

(2)金额的中文大写形式

给定一个浮点数,将其转换为金额的中文大写形式的字符串。例如,123. 45.表示为“壹佰贰拾叁元肆角伍分”。注意以下几种情况:
A.当金额为整数时,只表示整数部分,省略小数部分,并添加“整”字,例如123表示成“壹佰贰拾叁元整”。
B.当金额中含有连续的0时,只需写一个“零”即可。例如10005表示为“壹万零伍元整”。
C. 10的省略表示。例如110表示为“壹佰壹拾元整”,而10表示为“拾元整”。
D.100 表示为“壹佰元整”,1000 表示为“壹仟元整”等等。

package money;
import java.util.Scanner;
public class money {
	public static void main(String[] args)
	{
		char [] st = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾'};
		char [] ans = {'元','万','亿'};
		char [] ant = {'千','元','拾','佰'};
		Scanner ran1 = new Scanner(System.in);
		String s = ran1.nextLine();
		char [] moe = new char[10000]; 
		int wt = 0;
		  char [] all = s.toCharArray(); 
		  char [] wh = new char[20000];
		  int x1 = 0;
		  for(int i = 0; i<all.length; i ++ )
		  {
			  if(all[i]=='.') 
			  {
				  break;
			  }
			  wh[x1++]=all[i];
		  }
		  int t = x1;
		  for(int i = 0; i < t ; i ++ )
	      {
	    	  if(wh[i]=='0'&&(t-i)%4!=1)
	    	  {
	    		  if(wh[i+1]!='0') moe[wt++] = '零';
	    		   continue;
	    	  }
	    	  if((t-i)%4==1)
	    	  {
	    		  if(wh[i]!='0') moe[wt++]=st[wh[i]-'0'];
	    		  if(wh[i]!='0'||(i-1>=0&&wh[i-1]!='0')||(i-2>=0&&wh[i-2]!='0')||(i-3>=0&&wh[i-3]!='0'))
	    		  {
	    			  int k = (t-i)/4;
	    		      moe[wt++]=ans[k];
	    		  }
	    		  continue;
	    	  }
	    	  moe[wt++]=st[wh[i]-'0'];
	    	  int u = (t-i)%4;
	    	  moe[wt++]=ant[u];
	      }
		  if(t-4>=0&&wh[t-1]=='0'&&wh[t-2]=='0'&&wh[t-3]=='0'&&wh[t-4]=='0') moe[wt++]='元';
	      else if(t==1&&wh[t-1]=='0') {
	    	  moe[wt++]='零';
	    	  moe[wt++]='元';
	      }
		  if(!s.contains(".")) moe[wt++]='整';
		  else 
		 {
			    for(int i = x1+1;i < all.length ; i ++ )
	           {
	    	      if(i==x1+1)
	    	     {
	    		   moe[wt++] = st[all[i]-'0'];
	    		   moe[wt++] = '角';
	    		   continue;
	    	     }
	    	      else 
	    	     {
	    		  moe[wt++] = st[all[i]-'0'];
	    		  moe[wt++] = '分';
	    		  break;
	    	     }
	          }
	           if(x1+1==all.length-1)
	          {
	    	    moe[wt++]='零';
	    	    moe[wt++]='分';  
	          }
	   }
		ran1.close();
		System.out.print(moe);
	}
}

(3) 三子棋

编写程序,实现简单的三子棋游戏。在三子棋中,双方在33的棋盘中轮流下棋,- -方用表示,另一方用0表示。如果一方的3个棋子占据了同一行,同一列或者对角线,则该方获胜。如果棋盘已被棋子占满,但没有-方获胜则出现平局。在程序中,一方为用户,用户在界面上输入每次下棋的位置;另一方下棋的位置为计算机随机自动生成。

package tic_tac_toe;
import java.util.Random;
import java.util.Scanner;
class checkerboard
{
	private int x,y;
	public static char [][] st = {{'6','6','6','6'},{'6','6','6','6'},{'6','6','6','6'},{'6','6','6','6'}};
	public boolean  dos(int a,int b,char t)
	{
		x = a;
		y = b;
		if(check())
		  {
			change(t);
			
			return false;
		  }
		return true;
	}
    public boolean check()
	{
		if(st[x][y]=='6') return true;
		return false;
	}
    public void  change(char it)
    {
    	st[x][y] = it;
    }
    public int judge()
    {
    	for(int i = 1 ; i <= 3 ; i ++ )
    	{
    		
    		if(st[i][1]==st[i][2] && st[i][2]== st[i][3])
    		{
    			if(st[i][1]=='X' ) return 1;
    			else if(st[i][1]=='O') return 2;
    		}
    	}
    	for(int i = 1 ; i <= 3 ; i ++ )
    	{
    		
    		if(st[1][i]==st[2][i] && st[2][i]== st[3][i])
    		{
    			if(st[1][i]=='X' ) return 1;
    			else if(st[1][i]=='O') return 2;
    		}
    	}
    	if(st[1][1]==st[2][2] && st[2][2] == st[3][3]) 
    	{
    		if(st[1][1]=='X' ) return 1;
			else if(st[1][1]=='O') return 2;
    	}
    	if(st[3][1]==st[2][2] && st[2][2] == st[1][3]) 
    	{
    		if(st[3][1]=='X' ) return 1;
			else if(st[3][1]=='O') return 2;
    	}
    	boolean ends = true;
    	for(int i = 1 ; i <= 3 ; i ++ )
    	  for(int j = 1; j <= 3; j ++ )
    		   if(st[i][j]=='6')
    		   {
    			   ends = false;
    			   break;
    		   }
    	
    	if(ends) return 4;
    	return 3;
    	
    	
    	
    }
    public static void shows()
    {
    	for(int i = 1; i <= 3; i ++ )
    	 for(int j = 1 ; j <= 3 ; j ++ )
    	 {
    		 System.out.print(st[i][j]);
    		 if(j==3) System.out.println();
    	 }
    }
}
public class tic_tac_toe {
   public static void showit() {
	   System.out.println("游戏规则");
	   System.out.println("6表示尚未放棋子的位置");
	   System.out.println("X是玩家的棋子");
	   System.out.println("O是电脑的棋子");
   }
   public static void main(String [] args)
   {
	   Random computer = new Random();
	   Scanner ran1= new Scanner(System.in);
	   checkerboard player = new checkerboard();
	   checkerboard machine = new checkerboard();
	   showit();
	   while(true)
	   {
		   checkerboard.shows();
		   System.out.println("请输入x 坐标");
		   int x = ran1.nextInt();
		   System.out.println("请输入y 坐标");
		   int y = ran1.nextInt();
		   int x1 = computer.nextInt(3) + 1;
		   int y1 = computer.nextInt(3) + 1;
		   while(player.dos(x,y,'X'))
		   {
			   System.out.println("输入的位置不对啊");
			   checkerboard.shows();
			   System.out.println("请输入x 坐标");
			    x = ran1.nextInt();
			   System.out.println("请输入y 坐标");
			    y = ran1.nextInt();
		   }  
		   System.out.println("电脑正在考虑在什么地方下棋");
		   while(machine.dos(x1,y1,'O'))
		   {
			  System.out.println("电脑正在考虑中。。。。。");
			  x1 = computer.nextInt(3) + 1;
			  y1 = computer.nextInt(3) + 1;
		   }
		   int t = player.judge();
		    if(t==1){
		    	System.out.println("你赢了");
				  checkerboard.shows();
		    	break;
		    }
		    else if(t==2)
		    {
		    	System.out.println("你输了");
				  checkerboard.shows();
		    	break;
		    }
		    else if(t==3)
		    {
		    	System.out.println("游戏继续");
		    }
		    else 
		    {
		    	System.out.println("平局");
				 checkerboard.shows();
		    	break;
		    }
		   
	   }
	   ran1.close();
   }
}

举报

相关推荐

0 条评论