石头,剪刀,布 循环版(改进)

来源:互联网 发布:mac mini a1347 参数 编辑:程序博客网 时间:2024/04/29 03:17
问题及代码:
 
 
 
package first;import java.util.Scanner;public class text {public static void main(String[] args) {System.out.println("scissor for 0 , rock for 1 , paper for 2");String n[] = { "scissor", "rock", "paper" };Scanner get = new Scanner(System.in);Scanner getx = new Scanner(System.in);int a, b;int i = 0, j = 0;while (true) {int cp = (int) (Math.random() * 3);a = get.nextInt();while (!(a == 0 || a == 1 || a == 2)) {System.out.println("输入非法,请重新输入!");a = get.nextInt();}if (a - cp == 1)++i;else if (cp == a);else if (cp == 2 && a == 0)++i;else++j;System.out.println("you is " + n[a] + " the cp is " + n[cp]);System.out.println("");b = i;if ((i > 1) || (j > 1)) {if (b > 1)System.out.println("you win!");elseSystem.out.println("the cp win!");System.out.println("");System.out.println("是否继续游戏? 1 继续  0 终止");if (getx.nextInt() == 0)break;else {System.out.println("scissor for 0 , rock for 1 , paper for 2");i = 0;j = 0;}}}}}

 

 

 

 

 

运行结果:

 


 

0 0
原创粉丝点击