Tutorial 01_JAVA基本語法[實驗任務四]
阿新 • • 發佈:2017-10-08
空字符 image dialog inf info import 錯誤 cnblogs .cn
1、程序設計思想:循環六次隨機生成六個字符組合成一個隨機字符串,提示輸入判斷兩個字符串是否相等
2、程序流程圖
3、程序源代碼
1 //信1605-1 劉思翔 20163579 2 import javax.swing.JOptionPane; 3 public class RandomStr 4 { 5 public static void main(String[] args) 6 { 7 //定義空字符串 8 String result = ""; 9 String message = ""; 10 String str = "";11 //進行6次循環 12 for(int i = 0 ; i < 6 ; i ++) 13 { 14 //生成一個97~122的int型的整數 15 int intVal = (int)(Math.random() * 26 + 97); 16 //將intValue強制轉換為char後連接到result後面 17 result = result + (char)intVal; 18 } 19 20 while(true) //輸入驗證碼 21 { 22 str = JOptionPane.showInputDialog(null, "請輸入驗證碼:"+result,"輸入", JOptionPane.INFORMATION_MESSAGE); 23 24 if(str.equals(result))//判斷驗證碼是否正確 25 { 26 JOptionPane.showMessageDialog(null, "驗證成功", "提示", JOptionPane.INFORMATION_MESSAGE);27 break; 28 } 29 else 30 { 31 JOptionPane.showMessageDialog(null, "驗證碼輸入錯誤,請重新輸入", "提示", JOptionPane.WARNING_MESSAGE); 32 } 33 } 34 } 35 }
4、實驗結果截圖
Tutorial 01_JAVA基本語法[實驗任務四]