开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第24天,点击查看活动详情
✨欢迎关注🖱点赞🎀收藏⭐留言✒
🔮本文由京与旧铺原创,csdn首发!
😘系列专栏:java学习
💻首发时间:🎞2022年12月21日🎠
🀄如果觉得博主的文章还不错的话,请三连支持一下博主哦
🎧作者是一个新人,在很多方面还做的不好,欢迎大佬指正,一起学习哦,冲冲冲
🎀🎀🎀今日分享:这一年大概是我长这么大,最难熬的一年,也是让我成长最多的一年,感谢生活赐予我一场惊慌失措,但愿以后抬头由阳光
🐱💻导航小助手
零基础自学javase黑马课程第十八天 🐱💻导航小助手 🎠练习-用户登录✨练习-遍历字符串👚练习-统计字符个数🧣练习-拼接字符串🎨练习-字符串反转🎡练习-金额转换
🎠练习-用户登录
已知正确的用户名和密码,请用程序实现模拟用户登录,总共给三次机会,登陆之后,给出相应的提示
package com.itheima.test8;
import java.util.Scanner;
public class StringDemo3 {
public static void main(String[] args) {
String username="tm";
String password="89";
Scanner sc=new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("输入用户名");
String username1=sc.next();
System.out.println("输入密码");
String password1=sc.next();
if(username1.equals(username)&&password1.equals(password)){
System.out.println("登陆成功");
}else{
if(i==2){
System.out.println("锁定");
}else{
System.out.println("输入错误");
}
}
}
}
}
✨练习-遍历字符串
需求:键盘录入一个字符串,使用程序实现在控制台遍历该字符串
public char charAt(int index):根据索引返回字符
public int length():返回此字符串的长度
数组的长度:数据名.length
字符串的长度:字符串对象.length()
package com.itheima.test8;
import java.util.Scanner;
public class StringDemo5 {
public static void main(String[] args) {
//1.键盘录入一个字符串
Scanner sc=new Scanner(System.in);
String str=sc.next();
for (int i = 0; i < str.length(); i++) {
char c=str.charAt(0);
System.out.println(c);
}
}
}
👚练习-统计字符个数
package com.itheima.test8;
import java.util.Scanner;
public class StringDemo6 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入数");
String str=sc.next();
int bigCount=0;
int smallCount=0;
int numberCount=0;
for (int i = 0; i < str.length(); i++) {
char c=str.charAt(i);
if(c>='a'&&c<='z'){
smallCount++;
}else if(c>='A'&&c<='Z'){
bigCount++;
}else if(c>='0'&&c<='9'){
numberCount++;
}
}
System.out.println("小写字母有:"+smallCount+"个");
System.out.println("大写字母有:"+bigCount+"个");
System.out.println("数字字母有:"+numberCount+"个");
}
}
🧣练习-拼接字符串
定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在控制台输出结果。例如:数组为int[ ] arr={1,2,3}; 执行方法后的输出结果为:[1, 2, 3]
package com.itheima.test8;
public class StringDemo7 {
public static void main(String[] args) {
int[] arr={1,2,3};
String str=arrToString(arr);
System.out.println(str);
}
public static String arrToString(int[] arr){
if(arr==null){
return "";
}
if(arr.length==0){
return "[]";
}
String result="[";
for(int i=0;i<arr.length;i++){
if(i==arr.length-1){
result=result+arr[i];
}else{
result=result+arr[i]+", ";
}
}
result=result+"]";
return result;
}
}
🎨练习-字符串反转
定义一个方法,实现字符串反转,键盘录入一个字符串,调用该方法后,在控制台输出结果,例如,键盘录入abc,输出结果cba
package com.itheima.test8;
public class StringDemo8 {
public static void main(String[] args) {
reverse("abc");
}
public static String reverse(String str){
for (int i = str.length() - 1; i >= 0; i--) {
char c=str.charAt(i);
System.out.println(c);
}
return "";
}
}
🎡练习-金额转换
package com.itheima.test8;
import java.util.Scanner;
public class StringDemo9 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int money;
while(true){
System.out.println("请录入一个数");
money=sc.nextInt();
if(money>=0&&money<=9999999){
break;
}else{
System.out.println("金额无效");
}
}
String moneyStr="";
while(true){
int ge=money%10;
String capitalNumber=getCapitalNumber(ge);
moneyStr=capitalNumber+moneyStr;
money=money/10;
if(money==0){
break;
}
}
int count=7-moneyStr.length();
for (int i = 0; i < count; i++) {
moneyStr="零"+moneyStr;
}
System.out.println(moneyStr);
String[] arr={"佰","拾","万","仟","佰","拾","元"};
String result="";
for (int i = 0; i < moneyStr.length(); i++) {
char c=moneyStr.charAt(i);
result=result+c+arr[i];
}
System.out.println(result);
}
public static String getCapitalNumber(int number){
String[] arr={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
return arr[number];
}
}