guessgame.java
来自「精通JSP-Web开发技术与典型应用(光盘)」· Java 代码 · 共 81 行
JAVA
81 行
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 + =
减小字号Ctrl + -
显示快捷键?