JAVA猜数字1到100游戏

来源:互联网 发布:只用身份证的贷款软件 编辑:程序博客网 时间:2024/04/29 22:24

/**
 *
 * 猜数字游戏
 */
package com.lovo;  //猜数字(1到100)游戏

import java.util.Scanner;

public class Tset04 {

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
  int counter = 0;
  int answer = (int) (Math.random() * 100 + 1);
  int yourAnswer;
  do {
   counter++;
   System.out.print("亲,你猜的数字是多少:");
   yourAnswer = sc.nextInt();
   if (yourAnswer == answer) {
    System.out.println("恭喜你,答对了!");
    System.out.println("亲。你猜了" + counter + "次");
   } else if (yourAnswer > answer) {
    System.out.println("亲,小一点!");
   } else {
    System.out.println("亲,大一点!");

   }
  } while (yourAnswer != answer);

 }
}

0 0
原创粉丝点击