【问题标题】:How do I convert Numbers into Letters (A = 0, B = 1, C = 2, etc...)如何将数字转换为字母(A = 0、B = 1、C = 2 等...)
【发布时间】:2018-11-09 17:40:26
【问题描述】:

实际上我很抱歉代码不完整我想这可能更好..我想要的是在算法完成后我希望能够打印出数字输出并将数字输出转换为字母.. 大多数可能就像密码一样

public static void main(String args[])
{
    Scanner scanner = new Scanner(System.in);
    String ct;
    String k;
    int sum = 0;

    System.out.print("Enter Text: ");
    ct = scanner.nextLine();
    int arr1[] = new int[ct.length()];

    for(int j = 0; j < ct.length(); j++)
    {
        arr1[j] += ( (int)ct.charAt(j) - 97);
        System.out.println("Value of Text at["+j+"]: "+arr1[j]);
    }


    //user input key
    System.out.print("Enter random word: ");
    k = scanner.nextLine();

    for(int i = 0; i < k.length(); i++)
    {
        System.out.println( (int)k.charAt(i) - 97 );
        sum = sum + ( +(int)k.charAt(i) - 97 );
    }
    System.out.println("Sum of all letter in the random word: "+sum);
    for(int i = 0; i < ct.length(); i++)
    {
        System.out.print(arr1[i]+ " ");
        arr1[i] += sum;
        if(arr1[i] > 25)
        {
            arr1[i] -= 25;
            if(arr1[i] > 25)
            {
                arr1[i] -= 25;
            }
        }
    }
    System.out.println();
    for(int i = 0; i < ct.length(); i++)
    {
        System.out.print(arr1[i]+ " ");
    }

}

输出

Enter Text: kind
Value of Text at[0]: 10
Value of Text at[1]: 8
Value of Text at[2]: 13
Value of Text at[3]: 3
Enter random word: evil
4
21
8
11
Sum of all letter in the random word: 44
10 8 13 3 
4 2 7 22 

【问题讨论】:

  • 您想反转代码/逻辑吗?那么,... + " " 的反转是什么?基本上就是String.valueOf(...),试试Integer.valueOf(...)的反面,我想你可以自己算出... - 97的反面。

标签: java string char ascii


【解决方案1】:

你可以使用一个字符数组 char[] letters = "ABCD".toCharArray();

你想要的字母在你的号码在数组中的位置。

System.out.println(letters[0]);

将打印“A”

这只是因为您希望 A 从 0 而不是 97 开始。

【讨论】:

    【解决方案2】:

    你可以创建一个方法:

    public String getCharacterFromInt(int i) {
        return String.valueOf((char)(i + 'A'));
    }
    

    你可以调用方法:

    System.out.println("Value: "+getCharacterFromInt(0));
    

    如果为 0,则返回 A 等等。

    【讨论】:

      【解决方案3】:

      如果你想做这样的事情,请告诉我。

      public class MainClass {
          public static void main(String[] args) {
          Scanner scanner=new Scanner(System.in);
          String s=scanner.nextLine();
          for(int i=0;i<s.length();i++){
              System.out.println("Value of the text at ["+i+"]: "+((int) s.charAt(i)-22)%25);
          }
      
        }
      }
      

      如果输入是:evil

      输出将是:

      Value of the text at [0]: 4

      Value of the text at [1]: 21

      Value of the text at [2]: 8

      Value of the text at [3]: 11

      【讨论】:

      • 是的,类似的东西,但你如何做相反的事情?如何将其转换回字母?
      • 为此,您将需要除法结果( (int) s.charAt(i)-22)/25 ) ,因此您可以使用适合它的地图将其存储在某个地方。那么很容易取回 25*3+4+22=101 的字符,即e(这里 3 是除法结果)....我认为它现在有帮助...如果它有效,请接受我的回答。
      猜你喜欢
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多