1. 程式人生 > >根據提示輸入一個驗證碼 並判斷正誤

根據提示輸入一個驗證碼 並判斷正誤

cnblogs 掃描 正則表達 rom tro 強制轉換 mat 空字符串 強制

源代碼:

import javax.swing.JOptionPane;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Yanzhengma {

public static void main(String[] args)
{
//定義一個空字符串
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2
String result = "";
//進行6次循環
for(int i = 0 ; i < 6 ; i ++)
{
//生成一個97~122的int型的整數
int intVal = (int)(Math.random() * 26 + 97);
//將intValue強制轉換為char後連接到result後面
result = result + (char)intVal;
}
firstNumber =
JOptionPane.showInputDialog( "請輸入如下六位驗證碼:" +result);
if(firstNumber.equals(result))
{
JOptionPane.showMessageDialog(
null, "正確 " ,"Results",// 用對話框輸出結果
JOptionPane.PLAIN_MESSAGE );
}
else
{
JOptionPane.showMessageDialog(
null, "錯誤" ,"Results",// 用對話框輸出結果
JOptionPane.PLAIN_MESSAGE );
}

// read in second number from user as a string

// convert numbers from type String to type int

//parseInt 是Javascript內置函數,
//原型為parseInt( String s , [ int radix ] ),用於解析一個字符串,並返回一個整數。

/*Scanner -是java類庫的一個基礎類,一個可以使用正則表達式來解析基本類型和字符串的簡單文本掃描器。

input -聲明的變量名

new Scanner() -給變量input分配空間,初始化,實例化

System.in -參數, 這裏就是獲取輸入流的意思*/
//Scanner input=new Scanner(System.in);


}

}

驗證截圖:

技術分享技術分享技術分享技術分享

根據提示輸入一個驗證碼 並判斷正誤