将数字转换为字母的程序

3
我正在尝试创建一个程序,能够实现以下功能: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进制的系统,但是当我需要AA时却得到了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个回答

2

从A到Z有26个字母。您的系统是基于26进制而不是27进制。

您可能需要执行以下操作:

  • x从0改为1。您的系统当前将0表示为空字符串,这可能会使您感到困惑。

  • 对商和余数进行模26运算,而不是模27。

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


Java 使用 Unicode,而非 ASCII。对于字符 'A',UTF-16 代码单元的值为 65(某些字符需要两个代码单元)。该值为一个 16 位数字。 - Tom Blodget

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循环,并且每次从quotient中减去1,以将定义域从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

谢谢!它完美地运行了。我应该想到那个哈哈。 - user2994363

0
创建一个包含字母的字符数组,例如char letters[] = new char[]{'A','B', ... }。编写一个简单的循环,例如do ... while(num > 0)。使用letters.length对数字进行取模,并将结果字符添加到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));
        }

    }

}

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

提示:正确缩进你的代码!


0
这个问题及其答案中,我认为有一个更简单的方法:将数字转换为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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接