- //猜字游戏
- package com.kxg_03;
- import java.util.Scanner;
- public class GuessNumberGame {
-
- public GuessNumberGame(){}
- public static void start()
- {
- System.out.println("欢迎体验猜数字游戏,您一共有8次机会!");
- int number = (int) (Math.random()*100)+1;
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入要猜的数值(1-100):");
- for(int x=0; x<8; x++)
- {
- if((7-x) == 0)
- {
- System.out.println("对不起,次数已用完!");
- }
- else
- {
- int guessNumber = sc.nextInt();
-
- if(guessNumber < number)
- {
- System.out.println("您猜的数:"+guessNumber+" 小了");
- }
- else if(guessNumber > number)
- {
- System.out.println("您猜的数:"+guessNumber+" 大了");
- }
- else
- {
- System.out.println("恭喜您,猜对了!");
- break;
- }
- }
- }
- }
- }
复制代码 |