没有获得正确的程序输出

时间:2016-08-05 05:50:45

标签: java

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程序中这是为了认证目的

1 个答案:

答案 0 :(得分:0)

我认为你不能将这么大的号码分配给int M.