在主方法中添加代码,调用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 09:36:56 浏览: 57
简单了解JAVA中类、实例与Class对象
5星 · 资源好评率100%
你需要先创建一个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);
}
}
```
阅读全文