赞
踩
import java.util.Scanner; class WhileTest1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("\n******************************************************"); System.out.println("\n\t\t猜数字小游戏"); System.out.println(" 规则:系统会随机给出一个100以内的正整数,你可以"); System.out.println(" 无限次输入100以内的正整数,猜对后游戏结束.系"); System.out.println(" 统会自动统计猜数字的次数,次数少的一方为赢\n"); System.out.println("******************************************************\n"); System.out.print("\n\t请输入您的姓名:"); String str1 = scan.next(); System.out.println("\n\t正在为" + str1 + "获取随机数......"); int random1 = (int)(Math.random() * 100 + 1); System.out.println("\n\t成功\n"); System.out.print("\t请输入一个数值:"); int input1 = scan.nextInt(); //************************************************** int sum1 = 1; while(random1 != input1) { if(random1 > input1){ System.out.println("\t数值过小,请重新输入\n"); } else{ System.out.println("\t数值过大,请重新输入\n"); } System.out.print("\t请输入一个数值:"); input1 = scan.nextInt(); sum1++; } System.out.println("\t恭喜你,猜对了!\n"); System.out.println("******************************************************"); System.out.print("\n\t请输入您的姓名:"); String str2 = scan.next(); System.out.println("\n\t正在为" + str2 + "获取随机数......"); int random2 = (int)(Math.random() * 100 + 1); System.out.println("\n\t成功\n"); System.out.print("\t请输入一个数值:"); int input2 = scan.nextInt(); //************************************************** int sum2 = 1; while(random2 != input2) { if(random2 > input2){ System.out.println("\t数值过小,请重新输入\n"); } else{ System.out.println("\t数值过大,请重新输入\n"); } System.out.print("\t请输入一个数值:"); input2 = scan.nextInt(); sum2++; } System.out.println("\t恭喜你,猜对了!\n"); System.out.println("******************************************************"); System.out.println("\t" + str1 + "," + "您一共猜了" + sum1 + "次"); System.out.println("\t" + str2 + "," + "您一共猜了" + sum2 + "次\n"); if(sum1 > sum2){ System.out.println("\t" + str2 + "获胜"); } else if(sum1 < sum2){ System.out.println("\t" + str1 + "获胜"); } else{ System.out.println("平局"); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。