⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guessgame.java

📁 张新曼 精通JSP Web 开发技术与典型应用 随书光盘源码
💻 JAVA
字号:
package ch05;

import java.util.*;

/***
 * 
 * @author qq
 *
 * 定义bean,猜价格游戏
 */

public class GuessGame {
   
  //私有成员,定义所需要的属性	
  int answer;
  int guess;
  boolean success;
  String info;
  int counter;

  //构造函数,主要用于产生随机数
  public GuessGame() {
    reset();
  }

  //成员函数,设置和调用成员属性,完成游戏功能 
  public void setGuess(String guess) {
    counter++;

    try {
    	this.guess = Integer.parseInt(guess);
    }
    catch (NumberFormatException e) {
    	this.guess = -1;
    }

    if (this.guess == answer) {
      success = true;
    }
    else if (this.guess == -1) {
      info = "出错,再猜一次!";
    }
    else if (this.guess < answer) {
      info = "您猜的价格小了!";
    }
    else if (this.guess > answer) {
      info = "您猜的价格大了!";
    }
    
    if(this.guess >1000){
    	info="请输入1到1000之间的数字!!";
    }
  }

  public boolean getSuccess() {
    return success;
  }

  public String getInfo() {
    return info;
  }

  public int getCounter() {
    return counter;
  }

  public int getAnswer(){
	return answer;
  }
  
  
  public void reset() {
	//产生随机数,控制在1到1000之间
    answer = Math.abs(new Random().nextInt() % 1000) + 1;
    success = false;
    counter = 0;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -