zoukankan      html  css  js  c++  java
  • 01Java语言基础

    [实验任务四]:

    1.程序设计思想

    根据RandomStr.java,随机生成6位字母,在对话框中输出,用户根据随机生成的验证码对应输入,程序根据用户输入的内容与系统随机生成的验证码字符比较,若相等,程序结束;若不相等,程序循环,重新生成6位验证码,用户重新输入,直到输入正确为止。

    2.程序流程图

    3.源程序

    import javax.swing.*;

    public class yanzhengma6wei {

           public static void main(String[] args)

           {

                  // TODO Auto-generated method stub

                  //定义一个空字符串

               String result;

               String shuru;

               do

               {

                      result = "";

                  //进行6次循环

                  for(int i = 0 ; i < 6 ; i ++)

                  {

                         //生成一个97~122的int型的整数

                         int intVal = (int)(Math.random() * 26 + 97);

                         //将intValue强制转换为char后连接到result后面

                         result = (String)(result + (char)intVal);

                  }

                  //输出随机字符串

                  shuru=JOptionPane.showInputDialog(null,result,"请输入验证码",JOptionPane.PLAIN_MESSAGE);

                  if(!shuru.equalsIgnoreCase(result))

                         JOptionPane.showMessageDialog(null,"输入验证码错误!请重新输入!","error",JOptionPane.PLAIN_MESSAGE);

               }while(!shuru.equalsIgnoreCase(result));

               System.exit( 0 );

           }

    }

    4.实现结果截图

    5.实验总结(包括调试过程中出现的错误等)

    系统生成的随机字符串赋值给String型变量result,用户输入的字符赋值给String型变量shuru,在比较两值是否相等时出现问题。不能直接用简单的if(shuru==result),因为字符串变量只是字符串的管理者,并非字符串本身,若想比较,需使用equals()函数,此处应改为if(shuru.equals(result))。

    [实验任务五]:(选做)猜数字游戏

    1.程序设计思想

    用random()函数random()*100+1随机生成1~100的整数,用户输入数字猜想,若猜想数字小于或大于随机数,分别对应输出“猜小了”或“猜大了”,用户重新猜想输入,如此循环,直至猜想与随机数相等,输出“猜对了”,程序结束。

    2.程序流程图

    3.源代码

    import javax.swing.*;

    import java.util.Random;

    public class guess {

           public static void main(String[] args) {

                  // TODO Auto-generated method stub

    int number=(int)(Math.random()*100+1);

    String shuru=JOptionPane.showInputDialog(null,"请输入1~100的整数:","Guess",JOptionPane.PLAIN_MESSAGE);

    int num=Integer.parseInt(shuru);

    do

    {

    if(num>number)

    {

           shuru=JOptionPane.showInputDialog(null,"猜大了"+" "+"请输入1~100的整数:","reguess",JOptionPane.PLAIN_MESSAGE);

           num=Integer.parseInt(shuru);

    }

    else if(num<number)

    {

           shuru=JOptionPane.showInputDialog(null,"猜小了"+" "+"请输入1~100的整数:","reguess",JOptionPane.PLAIN_MESSAGE);

           num=Integer.parseInt(shuru);

    }

    }while(num!=number);

    if(num==number)

    {

           JOptionPane.showMessageDialog(null,"猜对了!","success",JOptionPane.PLAIN_MESSAGE);

           System.exit( 0 );

    }

    }

    }

    4.运行结果截图

     

     

    5.编译错误分析

    用户输入的内容为String型,要强制转换为int型,才能和int类型的随机数number相比较,用Integer.parseInt(shuru)。 

  • 相关阅读:
    删除commit(暂存区)中的文件(git)
    bower安装使用以及git安装
    compass模块----Utilities----Sprites精灵图合图
    compass模块----Utilities
    compass模块----Helpers
    compass模块
    compass安装
    Sass@规则
    Sass函数--颜色函数--Opacity函数
    Sass函数--颜色函数--HSL函数
  • 原文地址:https://www.cnblogs.com/wang-jx/p/7636143.html
Copyright © 2011-2022 走看看