我的任务是编写一个程序,计算电话号码(仅数字)中重复出现的数字次数。这是我目前为止的代码和输出结果。
import java.util.Scanner;
public class PhoneNumber {
public static void main(String[] args) {
Scanner key= new Scanner(System.in);
String number;
int[] phoneArray = new int[10];
int one = 0;
System.out.println("Enter your phone number in the format (xxx)xxx-xxxx:");
number = key.nextLine();
for(int i = 0; i < phoneArray.length; i++) {
System.out.println("Count of " + i + "'s: " + number.charAt(i));
}
}
}
输出:
Enter your phone number in the format (xxx)xxx-xxxx:
(864)728-1638
Count of 0's: (
Count of 1's: 8
Count of 2's: 6
Count of 3's: 4
Count of 4's: )
Count of 5's: 7
Count of 6's: 2
Count of 7's: 8
Count of 8's: -
Count of 9's: 1
我不确定自己哪里做错了。
phoneArray
保持不变。 - 001charAt()
返回一个整数,它返回字符的 ASCII 代码,而不是像1、2、3等数字。ifc记得,数字‘1’的ASCII码为49。您需要将ASCII代码转换为所需的数字,并且还需要检查那些不是数字(如“)”和“-”)的代码。 - markspace