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

编写一个Java程序,提示用户输入一串字符串,要求字符串中必须要有字母(需要代码判断)
1.若不符合要求,则提示用户重新输入直至符合要求为止
2.若符合要求,则判断字符串中的大写字母出现次数并打印。
示例1:
请输入带字母的字符串:
123451818
输入错误,请重新输入带字母的字符串:
qwer123df
输入的字符串中含大写字母0个

请输入带字母的字符串:
ABC童装
输入的字符串含大写字母3个

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<str1.length();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+"个");
}
}
}
}

纯手打,望采纳

温馨提示:答案为网友推荐,仅供参考
相似回答