两个小的java程序,用于练习java基本语法

1、输入两个数,求其加减乘除。用窗口的形式呈现

 1 import javax.swing.JOptionPane;
 2 
 3 public class JJCC {
 4     public static void main(String[] args)
 5     {
 6         String firstNumber,secondNumber;
 7         int number1,number2;
 8         int he,cha,ji,shang;
 9         firstNumber = JOptionPane.showInputDialog
10                 ("input the first integer please");
11         secondNumber = JOptionPane.showInputDialog
12                 ("inout the second integer please");
13         number1 = Integer.parseInt(firstNumber);
14         number2 = Integer.parseInt(secondNumber);
15         he = number1 + number2;
16         cha = number1 - number2;
17         ji = number1 * number2;
18         shang = number1 / number2;
19         JOptionPane.showMessageDialog(null, "number1 + number2 is "
20                 +he+"\nnumber1 - number2 is "+cha+"\nnumber1 *"
21                         + " number2 is "+ji+"\nnumber1 / number2 is "+shang
22                         ,"Results", JOptionPane.PLAIN_MESSAGE);
23     }
24 }


 

2、生成一个6位的随机字符串验证码(以窗口的形式)(位数可改)

 1 import javax.swing.JOptionPane;
 2 
 3 public class RandomStr {
 4 //    public void createRandomStr(int weishu)
 5 //    {
 6 //        String str = "";
 7 //        for(int i=1;i<=weishu;i++)
 8 //        {
 9 //            //生成一个表示a~z的ASCII的随机数
10 //            int intValue = (int)(Math.random()*26+97);
11 //            //将此随机数转化为其对应的字母并连接在str后面
12 //            str = str + (char)intValue;
13 //        }
14 //    }
15 
16     public static void main(String[] args) {
17     //生成一个6位的由小写字母组成的随机字符串    
18         int weishu=6;
19         String str = "";
20         for(int i=1;i<=weishu;i++)
21         {
22             //生成一个表示a~z的ASCII的随机数
23             int intValue = (int)(Math.random()*26+97);
24             //将此随机数转化为其对应的字母并连接在str后面
25             str = str + (char)intValue;
26         }
27     //随机字符串生成完毕
28         String inputStr;
29         inputStr = JOptionPane.showInputDialog("请输入以下验证码\n"+str);
30         if(inputStr.equals(str))
31         {
32             JOptionPane.showMessageDialog(null, "验证成功");
33         }
34         else
35         {
36             JOptionPane.showMessageDialog(null, "抱歉,验证失败");
37         }
38     }
39 
40 }
原文地址:https://www.cnblogs.com/yychnbt/p/4856105.html