我如何将数字改为字符?

时间:2014-10-09 17:40:52

标签: java

我有一个java代码,我需要用字符而不是数字得到相同的结果。(1,2,3,4,5,6,7)=> (a,b,c,d,e,f,g),我如何更改代码,谢谢。

    int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
    int len = numbers.length;
    int numberOfPer = 0;
        for (int i = 0; i < len; i++) {
        for (int j = 0; j < len; j++) {
            for (int k = 0; k < len; k++) {
                for (int l = 0; l < len; l++) {
                    for (int m = 0; m < len; m++) {
                        for (int n = 0; n < len; n++) {
                            for (int o = 0; o < len; o++) {
                                if ((i != j) && (i != k) && (i != l)
                                        && (i != m) && (i != n) && (i != o)
                                        && (j != k) && (j != l) && (j != m)
                                        && (j != n) && (j != o) && (k != l)
                                        && (k != m) && (k != n) && (k != o)
                                        && (l != m) && (l != n) && (l != o)
                                        && (m != n) && (m != o) && n != o
                                        && (j - i != 1) && (k - j != 1)
                                        && (l - k != 1) && (m - l != 1)
                                        && (n - m != 1) && (o - n != 1)
                                        && (j - i != -1) && (k - j != -1)
                                        && (l - k != -1) && (m - l != -1)
                                        && (n - m != -1) && (o - n != -1)) {
                                    System.out.println(numbers[i] + ""
                                            + numbers[j] + "" + numbers[k]
                                            + "" + numbers[l] + ""
                                            + numbers[m] + "" + numbers[n]
                                            + "" + numbers[o]);
                                    numberOfPer++;

                                }
                            }
                                                    }
                                            }
                                    }
                            }
                    }
            }
            System.out.println("Number of codes: " + numberOfPer);

3 个答案:

答案 0 :(得分:4)

你可以这样做:

 (char) ('a' + i)

答案 1 :(得分:0)

首先尝试插入带有ascii代码的char:A是65,依此类推...... Psudo代码:

Read numbers: {1,2,3} //A,B,C
For each number, print out: 
//Like in C:
putch(numbers[] + 65); //putch outputs a character in ascii code. I dont know java much, but that should work.

答案 2 :(得分:0)

int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
char[] letters = new char[ numbers.length ];
for( int i = 0; i < numbers.length; ++i )
{
    if( 0 < numbers[i] && numbers[i] <= 26 )
       letters[i] = (char)( numbers[i] + 'a' - 1 ); // subtract 1 since you are using 1='a' not 0='a'
}