猜数字游戏

猜数字游戏

信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,编程时不能将其搞混。

posted @ 2017-10-05 23:30  博呀客  阅读(333)  评论(0编辑  收藏  举报