当前位置:懂科普 >

综合知识

> 字符密码怎么写java

字符密码怎么写java

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);

}

标签: 字符 java
  • 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/zonghezhishi/95mjmn.html