Java猜数字游戏

利用输入流,编写简单猜数字游戏。游戏规则:游戏开始系统任意产生一个大于0,小于100的随机数,玩家通过键盘输入猜这个随机产生的数字,猜10次。在10次中如果猜中,控制台打... 利用输入流,编写简单猜数字游戏。游戏规则:游戏开始系统任意产生一个大于0,小于100的随机数,玩家通过键盘输入猜这个随机产生的数字,猜10次。在10次中如果猜中,控制台打印出胜利的语句,否则显示失败。程序开始的时候读取文本文档的欢迎词打印在控制台上。猜数字过程中如果输入的不是数字,提示请输入数字
如果输入的数字超出范围,提示请输入0~100的整数。谢谢~急
展开
 我来答
匿名用户
2013-12-01
展开全部
public static void main(String[] args) {
// TODO 自动生成方法存根
System.out.println("欢迎进入猜数字游戏!您只有10次机会!猜的数字在0到100之间");
Random r = new Random();
int num = r.nextInt(100);

Scanner input = new Scanner(System.in);
int cai;
for (int i = 0; i < 10; i++) {
System.out.print("输入竞猜数字:");
cai = input.nextInt();
if (cai < 0 || cai > 100) {
System.out.println("数字在0到100之间");
continue;
}
if (cai == num) {
System.out.println("猜中数字,胜利了");
break;
} else {
System.out.println("没有猜中");
}
if (i == 9) {
System.out.println("时间到,竞猜失败");
}
}

}
aa
2024-08-05 广告
街机游戏官方下载方面,有多家优秀平台可供选择,如游迅网、游侠网等,它们都拥有丰富的游戏资源和快速的下载速度。大连优逸电子有限责任公司也推出了自己的街机游戏下载平台,我们致力于提供多样化的经典街机游戏,以及稳定、安全的下载服务。在选择下载平台... 点击进入详情页
本回答由aa提供
匿名用户
2013-12-01
展开全部
本人亲测 正确运行。
import javax.swing.JOptionPane;
public class GuessNumber
{
public static void main(String args[])
{
int realNumber=(int)(Math.random()*100)+1;
int yourGuess=0;
String str;
int count=0;//统计你的输入次数
str=JOptionPane.showInputDialog("请输入一个1至100之间的整数");
yourGuess=Integer.parseInt(str);
while(yourGuess!=realNumber)
{
if(yourGuess>realNumber)
{
str=JOptionPane.showInputDialog("猜大了,请再输入您的猜测:");
yourGuess=Integer.parseInt(str);
}
else if(yourGuess<realNumber)
{
str=JOptionPane.showInputDialog("猜小了,请再输入您的猜测:");
yourGuess=Integer.parseInt(str);
}
count++;
if(count==10)
break;
}
System.out.println("你一共输入了"+count+1+"次");
if(yourGuess==realNumber)
{
System.out.println("成功了,恭喜你");
}
else
System.out.println("输入十次都错,你是笨蛋!");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-12-01
展开全部
/**
*
*/

/**
* @author JIANG_YI
*
*/
import java.util.*;

public class GuessPlay {

/**
* @param args
*/
public static void main(String[] args) {
boolean IsFirst = true;
while (true) {
if (IsFirst) {
if (IsFirstStart(true)) {
System.out.println("您猜了 " + GetRandom() + " 次.");
IsFirst = false;
}
} else if (IsFirstStart(false))
System.out.println("您猜了 " + GetRandom() + " 次.");
}

}

//
public static int GetInputTimes() {

int InputNum = -1;
Scanner sa = new Scanner(System.in);

System.out.println("请您猜题.....");

try {
InputNum = sa.nextInt();

} catch (Exception e)

{
InputNum = -1;
}
return InputNum;

}

public static boolean IsFirstStart(boolean bo) {
boolean IsStart = false;
while (true) {
System.out.println("-------------------------------");
System.out.println("请选择....");
if (bo)

System.out.println("1.开始游戏.....");
else
System.out.println("1.重新游戏.....");
System.out.println("2.退出游戏 .....");

System.out.println("-------------------------------");

Scanner sc = new Scanner(System.in);
try {
switch (sc.nextInt()) {
case 1:
IsStart = true;

break;
case 2:
System.out.println("---谢谢你的参与!----");
System.exit(0);

}
} catch (Exception e) {
if (IsStart)
break;
}

}
return IsStart;

}

public static int GetRandom() {
int times = 0;
int result = 0;
Random random = new Random();
result = random.nextInt(100) + 1;

while (true)
{
int i = GetInputTimes();

if (i < result) {
System.out.println("大了点再来....");
times++;
} else if (i >result) {
System.out.println("小了点再来,加油哦......");
times++;
} else if (i == result) {
System.out.println("恭喜您答对了.......");
times++;

break;
}

}
return times;

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式