-2
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private int M;
private int size;
private int index;
private Scanner scanner;
private ArrayList<String> list;
private ArrayList<String> code;
private String codes;
private int[] number;
public Main()
{
init();
takeInput();
printOutput();
printResult();
}
public void init(){
M=100000;
scanner=new Scanner(System.in);
list=new ArrayList<String>();
code=new ArrayList<String>();
index=0;
}
public void takeInput(){
String word="";
System.out.println("Enter the size of the dictionary");
size=scanner.nextInt();
if(size<=0 || size>M){
System.exit(0);
}
codes=new String();
number=new int[size];
for(int i=0;i<=size;i++){
word=scanner.nextLine();
list.add(word);
splitLetter(word);
code.add(codes);
codes="";
word="";
}
}
public void printOutput(){
for(int i=0;i<=size;i++){
System.out.println(code.get(i));
}
}
public void splitLetter(String letter){
char c;
for(int i=0;i<letter.length();i++){
c=letter.charAt(i);
assignCode(c);
}
}
public void assignCode(char c){
if(c=='a' || c=='b' || c=='c')
codes=codes.concat(String.valueOf(2));
if(c=='d' || c=='e' || c=='f')
codes=codes.concat(String.valueOf(3));
if(c=='g' || c=='h' || c=='i')
codes=codes.concat(String.valueOf(4));
if(c=='j' || c=='k' || c=='l')
codes=codes.concat(String.valueOf(5));
if(c=='m' || c=='n' || c=='o')
codes=codes.concat(String.valueOf(6));
if(c=='p' || c=='q' || c=='r')
codes=codes.concat(String.valueOf(7));
if(c=='s' || c=='t' || c=='u' || c=='v')
codes=codes.concat(String.valueOf(8));
if(c=='w' || c=='x' || c=='y' || c=='z')
codes=codes.concat(String.valueOf(9));
}
public void printResult(){
for(int i=0;i<=size;i++){
for(int j=0;i<=size;i++){
if(i==j)
continue;
if(code.get(i).equals(code.get(j))){
number[i]+=1;
}
}
}
for(int i=0;i<number.length;i++){
System.out.println(number[i]);
}
}
public static void main(String[] args) {
new Main();
}
}
的適當的輸出雖然我已經增加了數陣,但仍然我在數陣列中的所有元素得到0值,請幫我在這java程序,這是爲證明目的沒有得到計劃
那麼爲什麼數組數組爲什麼它的所有元素在循環中賦值後仍然爲0 –
Wbat執行'Scanner.nextInt()'返回?對於失敗安全,在獲取下一個輸入之前,您可能需要檢查'scanner.hasNextInt()'和/或'scanner.hasNextLine()'。 – n247s