房天下 > 房天下问答 > 业主生活 > 其他
  • 高分求JAVA设计个小游戏

    (1)玩家可以选择猜数字或者字母(可忽略大小写),每次只能猜一个数字或字母;(2)玩家猜测过程中,玩家可以选择给予提示,提示以范围的形式出现,譬如:系统生成答案为字母Y,玩家猜测为B,那么会给出提示“对不起您猜测的字母应该大于B,请继续…”;(3)玩家只能猜错有限次(譬如最多5次),否则游戏失败;(4)玩家的猜测记录要存储(选做)。要求全部有备注先弄50分,以免浪费

    提问者:swj19821119

    发布于2011-01-25

共1个回答
  • 志在千里1 丨Lv 0
    import java.io.*;import java.util.Random;public class Guess { Random _rand; int _target; boolean _choise; int _try[]; int _times; public static void main(String[] args) { System.out.print("choose to guess a number or a letter:\n"); System.out.print("1. number\n"); System.out.print("2. letter\n"); System.out.print("your choise is : "); try { InputStreamReader in = new InputStreamReader(System.in); int choise = in.read(); in.read();in.read(); //把回车换行读进去 Guess myGuess = new Guess(choise == 49); int temp = 0; System.out.print("\n=============================\n\n"); while (myGuess.canGuess()) { System.out.print("your guess is : "); choise = in.read(); in.read();in.read(); //把回车换行读进去 temp = myGuess.guess(choise); if (temp == 1) { System.out.print(" try smaller one.\n"); } else if (temp == -1){ System.out.print(" try bigger one.\n"); } else { break; } } System.out.print("\n=============================\n\n"); if (temp != 0) { System.out.print("The answer is : "); myGuess.answer(); } else { System.out.print("Nice work.\n"); System.out.print("The answer is : "); myGuess.answer(); } } catch (IOException ioe) { return; } } public Guess(boolean choise) { _times = 0; _try = new int[5]; _rand = new Random(); _choise = choise; if (_choise) { System.out.print("you choose to guess a number.\n"); _target = _rand.nextInt(10); } else { System.out.print("you choose to guess a letter.\n"); _target = _rand.nextInt(26); } } public int guess(int choise) { if (_times == 4) { return 2; } else { if (_choise) { choise -= 48; } else if (choise < 96){ choise -= 65; } else { choise -= 97; } _try[_times++] = choise; if (choise == _target) { return 0; } else if (choise > _target) { return 1; } else { return -1; } } } public boolean canGuess() { return (_times < 4); } public void answer() { if (_choise) { System.out.print(_target); } else { System.out.print((char)(_target+65)); } System.out.print("\n"); }}
    +1 2011-01-25 举报
热门人气推荐
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。