将数字转换为字母的程序

时间:2014-09-11 20:43:17

标签: java base letters

我试图创建一个执行此操作的程序:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD .... ZZ AAA AAB AAC

我接近这个的方式很难说,但我会尝试解释

我尝试创建一个基础27系统并使A代表1,B-> 2 C-> 3,AA-> 28 问题是每27个字母我得到一个@代表0.

我也试过让A代表0并且有一个基础26系统但是当我需要它时它将是BA

public class aaa 
{
    public static void main(String args[]) 
{
    int counter=29;
    for(int x=0;x<=counter;x++)
    {   
        int quotient, remainder;
        String result="";
        quotient=x;

        while (quotient>0)
        {   
            remainder=quotient%27;
            result = (char)(remainder+64) + result;
            quotient = (int)Math.floor(quotient/27);

        }
    System.out.print(result+ " ");
           }
    }
}

打印出A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A @ AA AB

我希望程序能够执行此操作A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC

5 个答案:

答案 0 :(得分:2)

从A到Z有26个字母。你的系统是26,而不是27。

您可能想要做的是:

  • 从1开始x,而不是0.您的系统当前将0表示为空字符串,这可能会让您失望。

  • 取商数和余数模26,而不是27。

  • 将65('A'的ASCII值)添加到余数,而不是64('@'的ASCII值)。

答案 1 :(得分:1)

有26个可能的字母,因此当您使用%/运算符时,请使用26作为除数。

A必须代表1,否则序列将等同于:

0 1 2 ... 25 00 01 02...

但是,我们需要计算范围在0-25范围内。

通过修改代码,我将x作为1开始。

int counter=29;
for(int x=1;x<=counter;x++)
{

稍后在while循环中,我将其更改为do - while循环,每次从1减去quotient,将域名从1-26转移到0-25。另外,我向其余部分添加65'A'),以便将0映射到'A'

    do
    {
        remainder=(quotient - 1)%26;
        result = (char)(remainder+65) + result;
        quotient = (int)Math.floor((quotient - 1)/26);
    }
    while (quotient>0);

输出:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC

答案 2 :(得分:0)

创建一个包含char letters[] = new char[]{'A','B', ... }之类字母的char数组。写一个像do ... while(num > 0)这样的简单循环。使用letters.length修改数字,并将结果char添加到StringBuilder。将数字除以letters.length。当循环完成后,输出就完成了。

<强>更新

public class MagicNumbers
{
    private static final char[] LETTERS = { '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    private static final int LLEN = LETTERS.length;

    private static String toMagicString(final int num)
    {
        int temp = num < 0 ? -num : num;

        StringBuilder strB = new StringBuilder();

        do
        {
            strB.append(LETTERS[temp % LLEN]);
            temp /= LLEN;
        }
        while (temp > 0);

        if (num < 0)
        {
            strB.append('-');
        }

        return strB.reverse().toString();
    }


    public static void main(final String[] args)
    {

        for (int i = -28; i < 29; i++)
        {
            System.out.println(toMagicString(i));
        }

    }

}

答案 3 :(得分:0)

首先,正如已经提到的几个答案,有26个字母,所以使用基础26系统,而不是27个。

除此之外,将A打印为0而不是@,因此将(char)(remainder + 64)更改为(char)(remainder + 65)。您需要做的最后一件事是更改quotient = (int)Math.floor(quotient/27);,因为您要打印A,在此比例中为0,因此从中减去1并在商小于0时停止循环。

public class HelloWorld{

     public static void main(String []args){
        int counter=59;
        for(int x=0; x<=counter; x++)
        {   
            int quotient, remainder;
            String result="";
            quotient=x;

            while (quotient >= 0)
            {
                remainder = quotient % 26;
                result = (char)(remainder + 65) + result;
                quotient = (int)Math.floor(quotient/26) - 1;
            }
            System.out.print(result+ " ");
        }
     }
}

输出(请注意,输出开始时也没有空格):

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH

Ps:正确缩进代码!

答案 4 :(得分:0)

this question and its answers我认为有一个更简单的方法:将数字转换为基数27,然后用适当的字母替换每个字符(正如Pshemo的评论所指出的,你需要定义一个“零”字符......我假设_代表零。 HashMap将保留您要用于打印号码的自定义字符。

import java.util.HashMap;

public class BaseConverter
{
    /**
     * Converts an integer to base 27, and returns the string with the custom characters
     * defined in the map.
     */
    public static String convertToMyBase27(int a) {
        HashMap<Character, Character> m = new HashMap<>();
        m.put('0', '_'); // Or another Zero character
        m.put('1', 'A');
        m.put('2', 'B');
        m.put('3', 'C');
        m.put('4', 'D');
        m.put('5', 'E');
        m.put('6', 'F');
        m.put('7', 'G');
        m.put('8', 'H');
        m.put('9', 'I');
        m.put('a', 'J');
        m.put('b', 'K');
        m.put('c', 'L');
        m.put('d', 'M');
        m.put('e', 'N');
        m.put('f', 'O');
        m.put('g', 'P');
        m.put('h', 'Q');
        m.put('i', 'R');
        m.put('j', 'S');
        m.put('k', 'T');
        m.put('l', 'U');
        m.put('m', 'V');
        m.put('n', 'W');
        m.put('o', 'X');
        m.put('p', 'Y');
        m.put('q', 'Z');

        String ans = "";
        String s = Integer.toString(a, 27);
        for(char c : s.toLowerCase().toCharArray()) {
            ans += m.get(c);
        }
        return ans;
    }
}