所以我的問題是我需要讓用戶輸入一個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