码迷,mamicode.com
首页 > 其他好文 > 详细

猜数字游戏

时间:2017-10-06 00:03:41      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:math   false   语句   nbsp   static   有感   try   包含   []   

猜数字游戏

信1603  20163548  马雯秋

 

一、程序设计思想

1.首先用random函数使计算机产生一个1——100的随机数

2.设置布尔类型的变量fg用来判断输入数字是否符合整数类型的要求

3.利用try-catch语句来实现意外情况的判断

4.再定义布尔类型的flag变量实现没猜中后的重新猜测

5.while语句包含三种情况,直到猜对数字为止

 

二、程序流程图

 技术分享

三、源代码

import java.util.Scanner;

public class main {

       public static void main(String[] args)

       {

              int guess=(int)(Math.random()*100);//产生一个0——100的随机数

              Scanner input=new Scanner(System.in);

              System.out.println("请输入你所猜的数字");

              int num=0;

              boolean fg=false;

              while(!fg)

              {

                     try  //try出现错误时会执行catch语句

                     {

                            num=input.nextInt();//输入一个整数

                            fg=true;

                     }

                     catch(Exception e)

                     {

                            System.out.println("你输错了,请重新输入");

                            input.next();

                     }

              }

              boolean flag=false;//旗帜变量,实现没猜中后的重新猜测

              while(!flag)

              {

                     if(num==guess)

                     {

                            flag=true;

                            System.out.println("猜对了");

                     }

                     else if(num>guess)

                     {

                            System.out.println("猜大了");

                            boolean fg1=false;

                            while(!fg1)

                            {

                                   try

                                   {

                                          num=input.nextInt();

                                          fg1=true;

                                   }

                                   catch(Exception e)

                                   {

                                          System.out.println("你输错了,请重新输入");

                                          input.next();

                                   }//循环结构

                            }

                     }

                     else if(num<guess)

                     {

                            System.out.println("猜小了");

                            boolean fg2=false;

                            while(!fg2)

                            {

                                   try

                                   {

                                          num=input.nextInt();

                                          fg2=true;

                                   }

                                   catch(Exception e)

                                   {

                                          System.out.println("你输错了,请重新输入");

                                          input.next();

                                   }

                            }

                     }

              }

       }

}

 

四、运行结果截图

 技术分享

五、编译错误分析

1.Try和catch语句是同时使用的,不能将其分开,只有当try语句出现异常时才会执行catch语句,catch语句是用来捕捉异常情况的。

2.只要是boolean类型的变量,不管其初值为多少,放在while()中之后,如果有感叹号!就为false,反之,为true,编程时不能将其搞混。

猜数字游戏

标签:math   false   语句   nbsp   static   有感   try   包含   []   

原文地址:http://www.cnblogs.com/wenqiu1998/p/7630269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!