这个我的代码和运行结果,还想要的效果是按回车键后继续输入字符串进行查询第一个字母是否为大写,直到输入exit才无法输入,应该码什么代码上去呢???
import java.util.Scanner;
public class dome5 {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
for (; ; ) {
System.out.println("请输入一个字符串:");
String str = num.nextLine();
if (str.equals("exit"))
{
break;
} else {
System.out.println("输出远洋字符串:" + str);
char tj = str.charAt(0);
int i, j = 0;
if (tj > 65 && tj <= 90) {
for (i = 0; i < str.length(); i++) {
char d = str.charAt(i);
if (d >= 65 && d <= 90) {
j++;
}
}
System.out.println("该字符串一共有" + j + "个大写字符字母");
} else {
System.out.println("第一个字母不是大写字母!");
}
}
}
}
}
实现代码如下:
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
while (true) {
System.out.println("请输入一个字符串: ");
String str = num.nextLine();
System.out.println("输出原样字符串: " + str);
if (str.equals("exit")) {
break;
}
char tj = str.charAt(0);
int i, j = 0;
if (tj >= 65 && tj <= 90) {
for (i = 0; i < str.length(); i++) {
char d = str.charAt(i);
if (d >= 65 && d <= 90) {
j++;
}
}
System.out.println("该字符串一共有" + j + "个大写字母");
} else {
System.out.println("第一个字母不是大写字母");
}
}
}
/*
测试结果
请输入一个字符串:
a
输出原样字符串: a
第一个字母不是大写字母
请输入一个字符串:
AaA
输出原样字符串: AaA
该字符串一共有2个大写字母
请输入一个字符串:
AAAA
输出原样字符串: AAAA
该字符串一共有4个大写字母
请输入一个字符串:
exit
输出原样字符串: exit
Process finished with exit code 0
*/
while(true)
{
String str=num.netxline();
if(str=="exit")break;
}
System.out.println("请输入一个字符串: ");
String str = num.nextLine();
while(!str.equals("exit"))
{
System.out.println("输出原样字符串: " + str);
char tj = str.charAt(0);
int i, j = 0;
if (tj >= 65 && tj <= 90) {
for (i = 0; i < str.length(); i++) {
char d = str.charAt(i);
if (d >= 65 && d <= 90)
j++;
}
System.out.println("该字符串一共有" + j + "个大写字母");
} else {
System.out.println("第一个字母不是大写字母");
}
System.out.println("请输入一个字符串: ");
str = num.nextLine();
}
看到你已经收藏了我的博客,没问题吧