所以我的问题是我需要让用户输入一个string
。那么他们会输入一个他们想要统计的角色。所以程序应该是count how many times the character
他们输入的字符串会出现,这是我的问题。如果有人能给我一些关于如何做到这一点的信息,将不胜感激。字符串中的java字符
import java.util.Scanner;
public class LetterCounter {
public static void main(String[] args) {
Scanner keyboard= new Scanner(System.in);
System.out.println("please enter a word");//get the word from the user
String word= keyboard.nextLine();
System.out.println("Enter a character");//Ask the user to enter the character they wan counted in the string
String character= keyboard.nextLine();
}
}
真棒工作,我只是有一个问题,为什么是charVar = 0开始? –
它需要被初始化,因为它是一个字符。所有的字符也是整数,所以0看起来像任何值一样好。 – ifly6