String案例1
输入账号和密码,一共有三次输入机会。
package com.itheima;
import javax.swing.*;
import java.util.Scanner;
public class one_121 {
public static void main(String[] args) {
String admin = “xiaoming”;
String password = “123456”;
for (int i = 0; i < 3; i++) {
System.out.println("请输入账号:");
Scanner sc = new Scanner(System.in);
String admin1 = sc.nextLine();
System.out.println("请输入密码:");
Scanner sc1 = new Scanner(System.in);
String password1 = sc1.nextLine();
if((admin1.equals(admin))&&(password.equals(password1))) {
System.out.println("登陆成功");
break;
} else {
if(i == 2){
System.out.println("你没机会了");
}else{
System.out.println("账号或密码错误,你还有"+(2-i)+"次机会");
}
}
}
}
}
String案例2
输入字符串,并把其中的字符挨个输出出来
package com.itheima;
import java.util.Scanner;
public class one_122 {
public static void main(String[] args) {
System.out.println(“请输入字符串:”);
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
System.out.print("{ “);
for(int i=0;i<s1.length();i++){
if(i == s1.length()-1){
System.out.print(s1.charAt(i));
}else{
System.out.print(s1.charAt(i)+”,");
}
}
System.out.println(" }");
}
}
String案例3
统计输入的字符串里面大写,小写和数字的个数。
package com.itheima;
import java.util.Scanner;
public class one_123 {
public static void main(String[] args) {
System.out.println(“请输入:”);
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
int bigNumber = 0,smaNumber = 0,digNumber = 0;
for(int i=0;i<s1.length();i++){
if(‘a’<=s1.charAt(i)&&s1.charAt(i)<=‘z’){
smaNumber++;
}else if (‘A’<=s1.charAt(i)&&s1.charAt(i)<=‘Z’){
bigNumber++;
}else if (‘0’<=s1.charAt(i)&&s1.charAt(i)<=‘9’) {
digNumber++;
}
}
System.out.println("大写字母有:"+ bigNumber);
System.out.println("小写字母有:"+ smaNumber);
System.out.println("数字有:"+ digNumber);
}
}