编写一个Java程序,提示用户输入一串字符串,要求字符串中必须要有字母(需要代码判断) 求解:

2025-03-05 11:06:45
推荐回答(1个)
回答1:

package test;

import java.util.Scanner;

public class test {
public static void main(String[] args) {
while(true){
Scanner input=new Scanner(System.in);
System.out.print("请输入带字母的字符串:");

String str1=input.next(); 
    
int counA=0;
int couna=0;
        
for(int i=0;i if((byte)str1.charAt(i)>64&&(byte)str1.charAt(i)<91){
counA++;
}else if((byte)str1.charAt(i)>96&&(byte)str1.charAt(i)<123){
couna++;
}
}
if(counA==0&&couna==0){
System.out.println("输入错误,请重新输入带字母的字符串:");
}else if(counA==0){
System.out.println("输入的字符串中含大写字母0个");
}else{
System.out.println("大写字母有"+counA+"个");
}
}
}
}

纯手打,望采纳