在学会循环结构语句时就可以写的一个猜拳游戏小项目

package com.etc.for2;

import java.util.Scanner;

/**
 * 猜拳游戏规则:
 * 人或机器可以随机出石头、剪刀、布,
 * 若一方出石头,另一方出剪刀,则输出打印出石头方获胜,
 * 若一方出石头,另一方出布,则输出打印出布方获胜,  
 * 若一方出布,另一方出剪刀,则输出打印出剪刀方获胜,
 * 
 */
public class TestCaiQuan {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		ok:
		while(true){
			System.out.println("请你输出你要出的手势:(0:石头,1:剪刀,2:布)");
			int person=sc.nextInt();
			if(person!=0&&person!=1&&person!=2){
				System.out.println("你输入的手势有问题,请重新输入!");
				continue ok;
			}
			switch(person){
			   case 0:
				   System.out.println("你出的手势是:石头");
				   break;
			   case 1:
				   System.out.println("你出的手势是:剪刀");
				   break;
			   case 2:
				   System.out.println("你出的手势是:布");
				   break;
			}
			
			int machine=(int)(Math.random()*3);
			switch(machine){
			   case 0:
				   System.out.println("电脑出的手势是:石头");
				   break;
			   case 1:
				   System.out.println("电脑出的手势是:剪刀");
				   break;
			   case 2:
				   System.out.println("电脑出的手势是:布");
				   break;
			}
			
			if((person==0&&machine==1)||(person==1&&machine==2)||(person==2&&machine==0)){
				System.out.println("恭喜你,你赢啦!");
			}else if((person==0&&machine==2)||(person==1&&machine==0)||(person==2&&machine==1)){
				System.out.println("很遗憾,你输了,请再接再厉!");
			}else{
				System.out.println("你和电脑打成了平局!");
			}
			boolean flag=false;
			while(true){
				System.out.println("是否继续下一轮的猜拳游戏(Y:N):");
				String str=sc.next();
				if("N".compareTo(str)==0){
					flag=false;
					break;
				}else if("Y".compareTo(str)==0){
					flag=true;
					break;
				}else{
					System.out.println("你输入有误,请重新输入!");//如何实现重新输入功能
				}
			}
			
			if(flag==false){
				break;
			}
		}
	}

}

  

原文地址:https://www.cnblogs.com/1020182600HENG/p/6798581.html