A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© momoxixi 中级黑马   /  2015-11-1 20:00  /  1747 人查看  /  29 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 momoxixi 于 2015-11-1 20:02 编辑

自己做出来了,但是感觉做的太复杂,有没有更简洁的方法?我自己的代码如下
  1. import java.util.Scanner;

  2. /**
  3. * Created by mo on 15/11/1.
  4. *
  5. * 需求:猜数字小游戏,猜数范围为1-100,猜数次数上限为5次
  6. *      若猜错,则下次猜数的范围根据本次输入的数据缩小
  7. *      输入超过100或者小于1的,要提示输入错误,同样计入猜数次数
  8. *      最后输出正确答案
  9. */
  10. public class GuessNumber {

  11.     public static void main(String[] args) {
  12.         int number = (int)(Math.random()*100)+1; //生成1-100的随机数
  13.         int key0 = 1;   //定义猜数范围下限
  14.         int key1 = 100; //定义猜数范围上限
  15.         int count = 0;  //定义猜数次数

  16.         while (true){
  17.             //while循环,因为前四次和第五次的提示不一样,所以内部先判断是前四次还是第五次猜数
  18.             if (count < 4){       //若要更改猜数次数上限,将此处改为猜数次数-1
  19.                 Scanner  sc = new Scanner(System.in);
  20.                 System.out.println("请输入您猜的数字,范围为"+key0+"-"+key1);
  21.                 int key = sc.nextInt();
  22.                 count++;

  23.                 if (key == number){
  24.                     System.out.println("猜对了!");
  25.                     break;
  26.                 }else if(key < 1 || key > 100){
  27.                     System.out.println("您输入的数据有误,请重试");
  28.                 }else if(key < number){
  29.                     if (key  > key0){       //根据用户输入的数据判断下限是否需要改变
  30.                         key0 = key;
  31.                     }
  32.                     System.out.println("您猜错了,请继续");
  33.                 }else {
  34.                     if (key  < key1){       //根据用户输入的数据判断上限是否需要改变
  35.                         key1 = key;
  36.                     }
  37.                     System.out.println("您猜错了,请继续");
  38.                 }

  39.             }else {             //第五次猜数
  40.                 Scanner  sc = new Scanner(System.in);
  41.                 System.out.println("请输入您猜的数字,范围为"+key0+"-"+key1);
  42.                 int key = sc.nextInt();
  43.                 if (key == number){
  44.                     System.out.println("猜对了!");
  45.                     break;
  46.                 }else if(key < 1 || key > 100){
  47.                     System.out.println("您输入的数据有误,超过规定次数,游戏结束");
  48.                     break;
  49.                 }else if(key < number){
  50.                     System.out.println("您猜错了,超过规定次数,游戏结束");
  51.                     break;
  52.                 }else {
  53.                     System.out.println("您猜错了,超过规定次数,游戏结束");
  54.                     break;
  55.                 }

  56.             }

  57.         }
  58.         System.out.println("正确答案为:"+number);
  59.     }
  60. }
复制代码



29 个回复

倒序浏览
朋友聚会的时候可以用上
回复 使用道具 举报
晕了,明天细看~
回复 使用道具 举报
我就是跟着视频的打出来的。
回复 使用道具 举报
恩恩不错。有注释,很好啊!
回复 使用道具 举报
亲  最起码加个提示大小的啊  不然神仙也不好猜啊  毕竟是100个数字呢……
回复 使用道具 举报
  1. import java.util.Scanner;
  2. class ccc {
  3.         public static void main(String[] args) {
  4.                     int a = 0;
  5.                     Scanner sc = new Scanner(System.in);
  6.                         System.out.println("请输入姓名");
  7.                         String n = sc.nextLine();
  8.                         System.out.println("请输入一个整数,范围在1-100之间");
  9.                         int y =(int)(Math.random()*100)+1;
  10.                         while (true) {
  11.                  a++;
  12.                 int x = sc.nextInt();
  13.                                 if (x>y) {
  14.                                         System.out.println("大了");
  15.                                 }else if (x<y) {
  16.                                         System.out.println("小了");
  17.                                 }else  {
  18.                                         System.out.println("中了");       
  19.                                         break;
  20.                                 }               
  21.                         }
  22.                            System.out.println(n+"你猜了"+a+"次");
  23.                         if (a<3) {
  24.                                 System.out.println(n+"你的智商大于120");
  25.                         }else if (a>3&a<7) {
  26.                                 System.out.println(n+"你的智商一般");
  27.                         }else {
  28.                                 System.out.println(n+"你的智商低于室温...");
  29.                         }

  30.         }
  31. }
复制代码
回复 使用道具 举报
回复 使用道具 举报
jlq 中级黑马 2015-11-2 09:37:32
9#
you dian kan guo
回复 使用道具 举报
JYcainiao 发表于 2015-11-2 00:35
亲  最起码加个提示大小的啊  不然神仙也不好猜啊  毕竟是100个数字呢……

已经提示范围了啊
回复 使用道具 举报

需求是要在限定次数内猜完啊,超过次数直接终止而不是猜到猜中为止
回复 使用道具 举报
  1. import java.util.Scanner;
  2. class Test1_guessNum {
  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);        //创建键盘录入对象.
  5.                 System.out.println("请从键盘输入你所猜的数,范围在1-100之间: ");               
  6.                 int count = 0;
  7.                 int num = (int)(Math.random() * 100) + 1;
  8.                         while (true) {
  9.                                 int guessNum = sc.nextInt();
  10.                                 if (guessNum > 100 || guessNum < 1) {
  11.                                         System.out.println("哥们你是不是不识字,范围在1-100之间!");
  12.                                         return;
  13.                                 }
  14.                                 count ++;       
  15.                                 if (guessNum > num) {
  16.                                         System.out.println("大了");
  17.                                 }else if (guessNum < num) {
  18.                                         System.out.println("小了");
  19.                                 }else {
  20.                                         System.out.println("恭喜你,猜中了!");
  21.                                         break;
  22.                                 }       
  23.                         }

  24.                 if (count > 0 && count <= 3) {
  25.                         System.out.println("大人真乃神人也!");
  26.                 }else if (count > 3 && count <= 10) {
  27.                         System.out.println("大人的智商还有待提高!");
  28.                 }else {
  29.                         System.out.println("大人,地球不适合你了,请你回火星吧!");
  30.                 }
  31.         }
  32. }
复制代码
回复 使用道具 举报
liying 来自手机 中级黑马 2015-11-2 12:36:05
13#
头晕,这么长。。。
回复 使用道具 举报
这个是做窗口化视图的猜数游戏,能查看成绩排行的前5次
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5. public class GuessNum {
  6.         private Frame f;
  7.         private Label l;
  8.         private Button b1;//随机出数
  9.         private Button b2;//我猜
  10.         private Button b3;//查看以往成绩
  11.         private TextField t1;
  12.         private int ran = -1;        //随机数字
  13.         private int num;        //猜的数字
  14.         private int conut=1;        //记录次数
  15.         private File file;        //文件路径
  16.         private Dialog fd;//对话框
  17.         private Frame f2;//对话框窗口
  18.         private TextArea ta;
  19.         private static final GuessNum g = new GuessNum();
  20.         public static void main(String[] args){
  21.                 g.init();        //窗口初始化
  22.                 g.b1Lstener();        //b1按钮监听事件-随机出数
  23.                 g.t1KeyListener();//t1监听事件,屏蔽字母和字符,只允许输入数字
  24.                 g.windowListener();//窗口监听事件-关闭
  25.                 g.b2Listener();//b2按钮监听事件-我猜
  26.                 g.b3Listener();
  27.         }
  28.         public void init(){
  29.                 f = new Frame("猜数字游戏");
  30.                 l = new Label("我猜,我猜,我猜猜猜          ");
  31.                 t1 = new TextField(10);
  32.                 b1 = new Button("随机出数");
  33.                 b2 = new Button("我猜");
  34.                 b3 = new Button("查看以往成绩");
  35.                 f.setResizable(false);
  36.                 f.setBounds(600, 300, 300, 100);
  37.                 f.setLayout(new FlowLayout());
  38.                 f.add(l);
  39.                 f.add(b1);
  40.                 f.add(t1);
  41.                 f.add(b2);
  42.                 f.add(b3);
  43.                 f.setVisible(true);
  44.         }
  45.         public void b2Listener(){        //b2按钮监听事件-我猜
  46.                 g.b2.addActionListener(new ActionListener(){
  47.                         public void actionPerformed(ActionEvent e){
  48.                                 try{
  49.                                         g.num = Integer.parseInt(g.t1.getText());
  50.                                         if(g.num>g.ran){
  51.                                                 if(ran>=0){
  52.                                                 g.conut++;
  53.                                                 g.l.setText("猜大了");       
  54.                                                 }
  55.                                                 else{
  56.                                                         g.l.setText("你还没有点随机");       
  57.                                                 }
  58.                                         }
  59.                                         else if(g.num<g.ran){
  60.                                                 if(ran>=0){
  61.                                                 g.conut++;
  62.                                                 g.l.setText("猜小了");
  63.                                                 }
  64.                                                 else{
  65.                                                         g.l.setText("你还没有点随机");       
  66.                                                 }
  67.                                         }
  68.                                         else{
  69.                                                 g.l.setText("猜对了");
  70.                                                 save();
  71.                                                 g.conut = 1;
  72.                                                 g.ran = -1;
  73.                                         }
  74.                                 }
  75.                                 catch(NumberFormatException n){
  76.                                         g.l.setText("请输入你要猜的数字");
  77.                                 }
  78.                         }
  79.                 });
  80.         }
  81.         public void b3Listener(){
  82.                 g.b3.addActionListener(new ActionListener(){
  83.                         public void actionPerformed(ActionEvent e){
  84.                                 fd = new Dialog(f2,"成绩排行");
  85.                                 ta = new TextArea(30,22);
  86.                                 fd.setBounds(650, 300, 200, 150);
  87.                                 fd.setLayout(new FlowLayout());
  88.                                 fd.add(ta);
  89.                                 fd.setResizable(false);
  90.                                 file = new File("c:\\temp.txt");
  91.                                 BufferedReader br = null;
  92.                                 int n = 0;
  93.                                 try{
  94.                                         br = new BufferedReader(new FileReader(file));
  95.                                         String value = null;
  96.                                        
  97.                                         while((value = br.readLine()) != null){
  98.                                                 ta.append("第"+ ++n + ":        " + value +"次"+ "\n");
  99.                                         }
  100.                                 }
  101.                                 catch(IOException ee){
  102.                                        
  103.                                 }
  104.                                 finally{
  105.                                         try{
  106.                                                 if(br!=null)
  107.                                                         br.close();
  108.                                         }
  109.                                         catch(IOException es){
  110.                                                
  111.                                         }
  112.                                 }
  113.                                 fd.setVisible(true);
  114.                                 fdListener();
  115.                         }
  116.                 });
  117.         }
  118.         public void fdListener(){
  119.                 fd.addWindowListener(new WindowAdapter(){
  120.                         public void windowClosing(WindowEvent e){
  121.                                 fd.setVisible(false);
  122.                         }
  123.                 });
  124.         }
  125.         public void windowListener(){        //窗口监听事件-关闭
  126.                 g.f.addWindowListener(new WindowAdapter(){
  127.                         public void windowClosing(WindowEvent e){
  128.                                 System.exit(0);
  129.                         }
  130.                 });
  131.         }
  132.         public void t1KeyListener(){        //只允许输入数字,屏蔽其他键,比如abc之类的
  133.                 g.t1.addKeyListener(new KeyAdapter(){
  134.                         public void keyPressed(KeyEvent e){
  135.                                 int key = e.getKeyCode();
  136.                                 if(!(key>=KeyEvent.VK_0 && key<=KeyEvent.VK_9 ||
  137.                                                                 key==KeyEvent.VK_BACK_SPACE ||
  138.                                                                 key>=KeyEvent.VK_NUMPAD0 && key<=KeyEvent.VK_NUMPAD9))
  139.                                         e.consume();
  140.                         }
  141.                 });
  142.         }
  143.         public void b1Lstener(){        //b1按钮事件监听
  144.                 g.b1.addActionListener(new ActionListener(){
  145.                         public void actionPerformed(ActionEvent e){
  146.                                 g.l.setText("我猜,我猜,我猜猜猜          ");
  147.                                 g.random();
  148.                         }
  149.                 });
  150.         }
  151.         public void save(){//保存成绩
  152.                 BufferedWriter bw = null;
  153.                 BufferedReader brr = null;
  154.                 file = new File("c:\\temp.txt");
  155.                 ArrayList<Integer> al = new ArrayList<Integer>();
  156.                 try{
  157.                         bw = new BufferedWriter(new FileWriter(file,true));
  158.                         String b = null;
  159.                         brr = new BufferedReader(new FileReader(file));
  160.                         bw.write(conut+"");
  161.                         bw.newLine();
  162.                         bw.flush();
  163.                         while((b = brr.readLine())!=null){
  164.                                 al.add(Integer.parseInt(b));       
  165.                         }
  166.                         Collections.sort(al);
  167.                         try{
  168.                                 if(bw!=null)bw.close();
  169.                                 if(brr!=null)brr.close();
  170.                         }
  171.                         catch(IOException sse){
  172.                                        
  173.                         }
  174.                         bw = new BufferedWriter(new FileWriter(file));
  175.                         for(int a = 0;a<al.size();a++){
  176.                                 if(a < 5){
  177.                                 bw.write(al.get(a)+"");
  178.                                 bw.newLine();
  179.                                 }
  180.                         }
  181.                 }
  182.                 catch(IOException e){
  183.                        
  184.                 }
  185.                 finally{
  186.                         try{
  187.                                 if(bw!=null)
  188.                                         bw.close();
  189.                                 if(brr!=null)
  190.                                         brr.close();
  191.                         }
  192.                         catch(IOException eee){
  193.                                
  194.                         }
  195.                 }
  196.         }
  197.         public void random(){        //随机
  198.                  ran = (int)(Math.random()*100)+1;
  199.         }
  200. }
复制代码
回复 使用道具 举报
看到的当时就蒙了,等待我复制下来慢慢研究~
回复 使用道具 举报
作为Java的初学者,能写出让人看着头晕的代码我佩服= = 然后我想提一点,为什么楼主非要把程序段写到程序入口,为嘛不写个方法或者函数,在里面实现你猜数字的功能,然后在main方法中调用,以至于达到条理分明,更容易让人读懂呢?
回复 使用道具 举报 1 0
看来看去,还是14楼的比较厉害
回复 使用道具 举报
回复 使用道具 举报
孤单背影 来自手机 中级黑马 2015-11-3 21:32:26
19#
好玩吗?
回复 使用道具 举报
用折半查找的方法试试
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马