学习的时候写过一个猜数字的小游戏,和楼主分享一下- package cn.itcase;
- import java.util.Random;
- import java.util.Scanner;
- class CaiShuZi {
- public static void main(String[] args) {
- println("这是一个猜数字游戏,请输入任意数字开始!");
- int x,y;
- x = ran();
- y = 0;
- while (true){
- y = scr();
- cai(x,y);
- }
-
- }
- public static int ran(){
- Random r = new Random();
-
- return r.nextInt(100);
- }
- public static void cai(int x,int y){
-
-
- if (x==y){
- println("恭喜您,猜对了!!!");
- }
- if (x < y){
- println("您猜的数字大了,请重新再试!");
- }else if (x>y){
- println("您猜的数字小了,请重新再试!");
-
- }
-
- }
- public static int scr() {
- Scanner sc = new Scanner(System.in);
- return sc.nextInt();
- }
- public static void println(Object o){
- System.out.println(o);
- }
- public static void print(Object o){
- System.out.print(o);
- }
- }
复制代码 |