Scanner类和String类常见对象案例
需求:模拟登录, 给三次机会, 并提示还有几次。
import java.util.Scanner;
public class Rng {
public static void main(String[] args) {
String name = "admin";
String password = "123456";
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
System.out.println("请输入你的用户名");
String uname = sc.nextLine();
System.out.println("请输入密码");
String pwd = sc.nextLine();
if (uname.equals(name) && pwd.equals(password)) {
System.out.println("登录成功");
break;
} else {
if((3 - i)>0){
System.out.println("用户名或密码错误 你还剩余" + (3 - i) + "次机会");
}else{
System.out.println("你的次数用完,账户已被冻结");
}
}
}
}
}
结果1:
请输入你的用户名
admin
请输入密码
123456
登录成功
Process finished with exit code 0
结果2:
请输入你的用户名
admi
请输入密码
123456
用户名或密码错误 你还剩余2次机会
请输入你的用户名
admin
请输入密码
12345
用户名或密码错误 你还剩余1次机会
请输入你的用户名
admin
请输入密码
12356
你的次数用完,账户已被冻结
Process finished with exit code 0
需求:遍历字符串
public class Test {
public static void main(String[] args) {
String s = "时间不在于你用有多少,而在于你怎样使用";
for (int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
}
}
结果:
时
间
不
在
于
你
用
有
多
少
,
而
在
于
你
怎
样
使
用
Process finished with exit code 0
需求:字符串反转
public class MyTest2 {
public static void main(String[] args) {
举例:键盘录入 "abc"
反转结果:"cba"*/
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一段字符串");
String s = scanner.nextLine();
String str="";
for (int length = s.length()-1; length >= 0; length--) {
char ch = s.charAt(length);
str+=ch;
}
System.out.println(str);
}
}
结果:
abc
cba
Process finished with exit code 0
需求:把数组转换成字符串
public class Test{
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9,9};
System.out.println(show(arr));
}
public static String show(int[] arr){
String s="[";
for (int i = 0; i < arr.length; i++) {
if (i == (arr.length-1)){
s=s+arr[i]+"]";
}else{
s+=arr[i]+",";
}
}
return s;
}
}
结果:
[1,2,3,4,5,6,7,8,9,9]
Process finished with exit code 0
需求:
查找数组中指定元素的索引值
案例代码:查找23在数组中第一次出现的索引值
public class demo {
public static void main(String[] args) {
int[] arr = {98,23,16,35,72};
int t=show(arr);
System.out.println(t);
}
public static int show(int arr[]){
int t=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]==23){
t=i;
break;
}
}
return t;
}
}
结果:
1
Process finished with exit code 0
需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)*/
案例代码:
String str="asdfASFAEZSf5244dsfsdf555asdfa36425444asdfasdf";
int xiao=0;
int da=0;
int num=0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if(ch>='a'&&ch<='z'){
xiao++;
}else if(ch >= 'A' && ch <= 'Z'){
da++;
}else{
num++;
}
}
System.out.println("小写字符有"+xiao+"个");
System.out.println("大写字符有" + da + "个");
System.out.println("数字字符有" + num + "个");
}
}
结果:
小写字符有24个
大写字符有7个
数字字符有15个
Process finished with exit code 0
需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)*/
案例代码:
public class MyTest2 {
public static void main(String[] args) {
String str="abCDefG";
String s = str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
System.out.println(s);
}
}
结果:
Abcdefg
Process finished with exit code 0
需求:统计一个长字符串中某一个小串出现的次数
案例演1:
public class MyTest3 {
public static void main(String[] args) {
String maxStr = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
String minStr = "java";
int index = maxStr.indexOf(minStr);
int count = 0;
while (index != -1) {
count++;
maxStr = maxStr.substring(index + 4);
index = maxStr.indexOf(minStr);
}
System.out.println("java 出现了 "+count+" 次");
}
}
结果:
java出现了 5 次
Process finished with exit code 0
案例演示2:
public class MyTest5 {
public static void main(String[] args) {
String maxStr = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
String minStr = "java";
int length = maxStr.length();
int length1 = maxStr.replace(minStr, "").length();
System.out.println((length - length1) / 4);
}
}
结果:
5
Process finished with exit code 0