| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 775 人关注过本帖
标题:一个猜数字的游戏,有点问题,求助
只看楼主 加入收藏
dbgcarlos
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2005-2-7
收藏
 问题点数:0 回复次数:5 
一个猜数字的游戏,有点问题,求助

这是本人看完循环之后写出来的程序,有点错误改了很久没有改出来,请各位大虾帮帮忙
这是一个猜数字的游戏,数字随机产生,然后输入数字直到猜对为止。

package com.shijian.ch04;

import java.math.*;
import java.io.*;

public class Guess
{
int i,num;
char s;

public void beginG() throws java.io.IOException
{
while(true)
{
System.out.println("Do you want to play the number-guess game?");
System.out.println("(y/n)?");
s=(char)System.in.read();
//System.out.println(s);
switch(s)
{
case 'y':GuessN();
case 'n':System.exit(1);
}
}
}

public void GuessN() throws java.io.IOException
{
num=(int)(100*Math.random());
for(;;)
{
System.out.println("Please input a number between 0-100:");
System.out.print("What is it:");
do
{
i=System.in.read();
}while(i=='\n'||i=='\r');
//System.out.println(i);
if(i==num)
{
System.out.println("you are talent");
break;
}
else
{
if(i<num)
{
//System.out.println("the "+i+" is lower than "+num+",try it again");
System.out.println("the "+i+" is lower than the nummber,try it again");
continue;
}
else
{
//System.out.println("the "+i+" is larger than "+num+",try it again");
System.out.println("the "+i+" is larger than the number,try it again");
continue;
}
}
}
}
}

package com.shijian.ch04;

import java.io.*;

public class TestGuess
{
public static void main(String args[]) throws java.io.IOException
{
System.out.println("Welcome to the game");
Guess g;
char s;

while(true)
{
g=new Guess();
g.beginG();
System.out.println("Would you like to play it again?");
s=(char)System.in.read();
switch(s)
{
case 'y':continue;
case 'n':System.exit(1);
}
}
}
}
谢谢

搜索更多相关主题的帖子: 数字 Guess 游戏 java import 
2006-12-24 10:05
WestNet
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2006-10-31
收藏
得分:0 
有什么问题?

不灰心等待,痛苦也忍耐。
2006-12-24 11:40
dbgcarlos
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2005-2-7
收藏
得分:0 

我输入10,i就变成了49;输入21,i就变成了50,这是怎么回事啊?


2006-12-25 07:22
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
它读入的是ascii码

1的ASCII码是49,2的ASCII码是50

你应该用一个完整的流来读完整的数

你可以用Scanner或者用别的来读它

可惜不是你,陪我到最后
2006-12-25 08:59
dbgcarlos
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2005-2-7
收藏
得分:0 
请问版主应该怎么改比较好,我的jdk是1.4.2的,谢谢!!

2006-12-25 22:06
ttrr550
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-11-5
收藏
得分:0 
用java.io;下的
BufferedReader
它有readLine()方法 读进来String 再转换就可以了
例如:
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String temp = br.readLine();
... 转换....就可以了

[此贴子已经被作者于2006-12-25 22:29:28编辑过]


2006-12-25 22:23
快速回复:一个猜数字的游戏,有点问题,求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012125 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved