在主方法中添加代码,调用count类 public class StringDemo4 { public static void main(String[] args) { } public void count(){ int num1=0; int num2=0; int num3=0; // char c='9'; // System.out.println((int)c); byte[] bytes =str.getBytes(); for (byte b:bytes){ if(b>=65&&b<=90){ //A65 Z 90 //a97 z 122 num1++; } else if (b>=97&&b<=122 ) { num2++; } else if (b>=49&&b<=57) { num3++; } } System.out.println(num1); System.out.println(num2); System.out.println(num3); } }
时间: 2023-12-14 08:36:56 浏览: 26
你需要先创建一个StringDemo4的对象,然后通过该对象调用count()方法。代码如下:
```
public class StringDemo4 {
public static void main(String[] args) {
StringDemo4 demo = new StringDemo4();
demo.count();
}
public void count(){
int num1=0;
int num2=0;
int num3=0;
byte[] bytes =str.getBytes();
for (byte b:bytes){
if(b>=65&&b<=90){
num1++;
} else if (b>=97&&b<=122 ) {
num2++;
} else if (b>=49&&b<=57) {
num3++;
}
}
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
}
}
```
相关问题
public class T01 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入一个大字符串:"); String bigString = scan.next(); System.out.println("请输入一个大字符串:"); String smallString = scan.next(); int count = countNum(bigString,smallString); System.out.println("小字符串在大字符串中出现的次数为" + count + "次"); } public static int countNum(String bigString, String smallString) { int count = 0; int index = 0; while ((index = bigString.indexOf(smallString,index)) != -1) { count ++ ; index += smallString.length(); } return count ; } }要求:详细注释每一句代码的含义
/**
* T01 类是一个用于统计大字符串中小字符串出现次数的工具类
*/
public class T01 {
public static void main(String[] args) {
// 创建一个 Scanner 对象用于从控制台读取输入
Scanner scan = new Scanner(System.in);
// 提示用户输入一个大字符串
System.out.println("请输入一个大字符串:");
// 读取用户输入的大字符串
String bigString = scan.next();
// 提示用户输入一个小字符串
System.out.println("请输入一个小字符串:");
// 读取用户输入的小字符串
String smallString = scan.next();
// 调用 countNum 方法统计小字符串在大字符串中出现的次数
int count = countNum(bigString, smallString);
// 输出小字符串在大字符串中出现的次数
System.out.println("小字符串在大字符串中出现的次数为" + count + "次");
}
/**
* 统计小字符串在大字符串中出现的次数
* @param bigString 大字符串
* @param smallString 小字符串
* @return 小字符串在大字符串中出现的次数
*/
public static int countNum(String bigString, String smallString) {
int count = 0; // 计数器,用于统计小字符串出现的次数
int index = 0; // 索引,用于记录上一次匹配到的位置
// 循环查找小字符串在大字符串中的位置,直到找不到为止
while ((index = bigString.indexOf(smallString, index)) != -1) {
count++; // 每次找到小字符串,计数器加一
index += smallString.length(); // 将索引移动到上一次匹配结束的位置后面
}
return count; // 返回小字符串在大字符串中出现的次数
}
}
public class Test { public static void main(String[] args) { String s = "Hi, Good Morning"; //这里m方法的功能是检测大写字母的数量 //3 System.out.println(m(s)); } public static int m(String s) { int count = 0; for (int i = 0; i < s.length(); i++) if (Character.isUpperCase(s.charAt(i))) count++; return count; } }
这段代码是一个简单的 Java 程序,其中包含了一个名为 m 的方法,用于计算字符串中大写字母的数量。在 main 方法中,定义了一个字符串 s,然后调用 m 方法计算 s 中大写字母的数量并打印结果。
具体来说,这个程序的执行流程如下:
1. 在 main 方法中定义了字符串 s,其值为 "Hi, Good Morning"。
2. 调用 m 方法,将字符串 s 作为参数传入。
3. 在 m 方法中,定义了一个整型变量 count,初始值为 0。
4. 使用 for 循环遍历字符串 s 中的所有字符,如果字符是大写字母,就将 count 加 1。
5. 循环结束后,返回 count 的值。
6. m 方法返回后,将大写字母的数量打印到控制台。
最终输出结果为 3,因为字符串 "Hi, Good Morning" 中有三个大写字母。