網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > 綜合知識 > 

字元密碼怎麼寫java

欄目: 綜合知識 / 發佈於: / 人氣:2.03W
1.用JAVA編寫輸入用戶名和密碼

import java.util.Scanner;

字元密碼怎麼寫java

public class Test

{

public static void main(String []args)

{

String str1="青";

int num1=123;

Scanner scanner=new Scanner(System.in);

System.out.print("請輸入名字:");

String str=scanner.next(); //獲取字元串值

System.out.println("您輸入的名字是:"+str);

if(str1==str)

{

System.out.println("對不起,你不是青2");

}

else

{

System.out.print("請輸入密碼:");

int num=scanner.nextInt(); //獲取整數值

System.out.println("您輸入的密碼是:"+num);

if(num1==num)

{

System.out.println("歡迎你,青");

}

else

{

System.out.println("對不起,你不是青1");

}

}

}

}

2.java中如何輸入一個字元

import java.util.*;

public class Test_01

{

public static void main(String[] args)throws Exception

{

System.out.println("請輸入一個字元");

char c=(char)System.in.read();

System.out.println(c);

}

}

擴展資料:

還可以輸入字元串,輸入字元串的方法

import java.io.*;

public class Test

{

public static void main(String[] args) throws IOException

{

BufferedReader buf = new BufferedReader (new InputStreamReader(System.in));

BufferedWriter buff = new BufferedWriter(new FileWriter("abc.txt"));

String str = buf.readLine();

while(!str.equals("exit"))

{

buff.write(str);

buff.newLine();

str = buf.readLine();

}

buf.close();

buff.close();

}

}

3.Java 寫一個輸入密碼的小程序

已完成,複製粘貼即可。

import java.util.Scanner; public class YuGiOh { private static void checkPass ( String password ) { String regp = ".*[A-Z].*"; String rego = ".*[a-z].*"; String regq = ".*d.*"; String regs = ".*[`-!@#$%^&*()_+=[{}];:,.<>/?].*"; String regex = "^[a-zA-Zd`-!@#$%^&*()_+=[{}];:,.<>/?]{6,}$"; String result = ""; if (password.length () < 6) { result += "tt-contain at least six characters. Your password is only " + password.length () + " characters long.n"; } if (!password.matches (regp)) { result += "tt-contain at least one uppercase letter.n"; } if (!password.matches (rego)) { result += "tt-contain at least one lowercase letter.n"; } if (!password.matches (regq)) { result += "tt-contain at least one number.n"; } if (!password.matches (regs)) { result += "tt-contain at least one of the following special characters: `!@#$%^&*()_+=[{}];:,.<>/?n"; } else if (!password.matches (regex)) { result += "tt-not contain an invalid character. The valid special characters: `!@#$%^&*()_+=[{}];:,.<>/?n"; } if (!"".equals (result)) { System.out.println ("Your password must:n" + result); } else { System.out.println ("Your valid password is: " + password); } System.out.println (); } public static void main ( String[] args ) { Scanner scanner = new Scanner (System.in); while (true) { System.out.print ("Enter a valid password: "); String password = scanner.nextLine (); checkPass (password); } } }。

4.java中如何輸入一個字元

暈,剛纔回答了你的問題,題目沒了……

實在不大想把代碼重新再寫一遍了。實際上很簡單。Scanner 是可以用的。讀進來的是字元串,比如說儲存在 str。

str.charAt(0); 就是第一個字元。括號裏的數字就是 index。把字元串就當數組看好了。

還有一個解決方案就直接用 char c = (char)new BufferedReader(new InputStreamReader(System.in)).read();

就可以讀取你輸入的第一個字元。

然後有了字元你就隨便處理好了。比如可以用 switch 語句:

switch (c) {

case 'A':

// do something

case 'B':

// do something

}

---------------------------------------

初學者,我就把代碼再寫一遍吧:

import java.io.*;

public class Demo {

public static void main (String args[]) {

char c = 0;

try {

c = (char)new BufferedReader(new InputStreamReader(System.in)).read();

} catch (IOException ioe) {

System.exit(0);

}

switch (c) {

case 'A':

System.out.println("It is A.");

break;

case 'B':

System.out.println("It is B.");

break;

}

}

}

5.java如何輸入一個字元

import java.util.Scanner; //引入Scanner類該類封裝了接收用戶輸入參數的一些操作

寫方法體

public static void main(String[] arg)

{

Scanner input=new Scanner(System.in); //定義一個從系統快取讀取數據的對偶

String str=scanner.next(); //從快取中讀出數據

System.out.println(str);

}

Tags:java 字元