Java写剪刀石头布

剪刀石头布

输入语句
Scanner input = new Scanner(System.in);//input是一个变量,名称是可以任意更改的如Scanner a = new Scanner(System.in);
int num = input.nextInt();//这里的input与上面的input对应,如果上面变量名改变则这里也要改变
定义数组
数据类型 + [ ] + 数组名 = new + 数据类型 + [数组长度]//数组长度一定不要忘记,极其重要;或者如果后面要列举数组里的元素,数组长度一定不要写进去
数据类型 + [ ] + 数组名 = new + 数据类型 + [ ]={
    
     ....... }//这时候[]里不能写数组长度    

石头剪刀布小游戏

scissors(0) rock (1) paper (2)
Math.random()函数的用法
int num = (int)(Math.random()*3+1);//这时num随机生成的数就是1/2/3
        // **这里的为什么要写一个(int)我现在不是很明白**
							//这里的*3代表着从0开始顺序随机生成三个数(跟数组的存储有点像都是从0开始)
							  //这里的+1代表着下限0的提高,+1就是从1开始随机顺序生成三个数,+2就是从二开始随机顺序生成三个数
将计算过程写成子函数,子函数的作用就是封装计算过程,等到主函数要用的时候拿出来,避免了主函数中计算过程出现多次的冗余问题
但这时候就出现一个问题,本来再主函数中定义的变量如今在子函数中没办法用,——那这时候就把变量定义拿到子函数中去,因为作为主函数的一部分(子函数为void类型,不需要返回什么,就只是把本来要多次写进主函数里的内容拿出来,等到用的时候用一个函数语句代替就好,不用每次用都把计算过程写一遍),把变量定义在子函数中就等于把函数声明的头去掉,把内容放进主函数中,跟直接写进主函数中效果是一样的;如果遇到需要先声明变量才能进入语句的循环判断情况就用先执行再循环的do-while语句
初始化界面和连续多次玩游戏出现的界面是不一样的,所以第一遍要单独写出来,之后情况相同的界面显示或者语句显示再用循环语句,因为所有部分的计算过程是一样的,所以把计算过程写成一个函数
可能会担心计算过程中如果有声明变量,那么每调用一次就再声明一次变量会不会有问题呢,确实可能会有问题,对于一些连续的过程就会有问题,但是对于石头剪刀布这种每一次的初始情况都独立、与上一次的结果不相关、对下一次的结果不产生影响的小游戏,就可以一次一次的重新声明变量把变量初始化。(重复声明并且覆盖原有变量的行为不知道是不是很耗费空间的啊

下面把代码附上

package javaLab2;

import java.util.Scanner;

public class Paper012 {
    
    
//	public static int ran,num;
	
	public static void compute() {
    
    
		
		int ran,num;
		ran = (int)(Math.random()*3);
		System.out.println("please enter a number:");
		Scanner input = new Scanner(System.in);	
		num = input.nextInt();
		
		if(num > 2 || num < 0)
		{
    
    
			System.out.println("input error,please enter again");
			compute();
		}
		//游戏过程计算
		int a = 0;//用来记录次序
			
		if(ran == 0 && num == 0)
			a = 1;
		if(ran == 0 && num == 1)
			a = 2;
		if(ran == 0 && num == 2)
			a = 3;
		if(ran == 1 && num == 0)
			a = 4;
		if(ran == 1 && num == 1)
			a = 5;
		if(ran == 1 && num == 2)
			a = 6;
		if(ran == 2 && num == 0)
			a = 7;
		if(ran == 2 && num == 1)
			a = 8;
		if(ran == 2 && num == 2)
			a = 9;
		
		switch(a)
		{
    
    
		case 1:
			System.out.println("The computer selects scissors. You select scissors too. It is a draw.");
			break;
		case 2:
			System.out.println("The computer selects scissors. You select rock.You win!");
			break;
		case 3:
			System.out.println("The computer selects scissors. You select paper.You are lost!");
			break;
		case 4:
			System.out.println("The computer selects rock. You select scissors.You are lost!");
			break;
		case 5:
			System.out.println("The computer selects rock. You select rock too. It is a draw.");
			break;
		case 6:
			System.out.println("The computer selects rock. You select paper.You win!");
			break;
		case 7:
			System.out.println("The computer selects paper. You select scissors.You win!");
			break;
		case 8:
			System.out.println("The computer selects paper. You select rock.You are lost!");
			break;
		case 9:
			System.out.println("The computer selects paper. You select paper too. It is a draw.");
			break;
		}
	}
	
	public static void main(String[] args) {
    
    
			System.out.println("scissors (0), rock (1), paper (2)");			
			Scanner input = new Scanner(System.in);	
			System.out.println("Computer will generate a random number\nThe Game Begins");
			
			int ran;
			int num;
			compute();	
			int b ;//判断是否要继续玩的变量
			do
			{
    
    
				System.out.println("Do you still want to play? yes(1),no(0) ");
			
				b = input.nextInt();
				if(b == 1)
				{
    
    
					compute();
				}
			}while(b != 0);
	}
}

猜你喜欢

转载自blog.csdn.net/ahahayaa/article/details/120631731