简体   繁体   English

将一串字母转换为数字

[英]Converting a string of letters into numbers

So I'm new to computer science and one of the things we've been asked to do is to attempt to create a program that firstly converts a word inputted by the user into a string of numbers, with a=1, b=2, z=26 etc. along with some other things which I wont go into now as they're extension. 因此,我是计算机科学的新手,我们被要求做的一件事就是尝试创建一个程序,该程序首先将用户输入的单词转换为一串数字,其中a = 1,b = 2 ,z = 26等,以及随着扩展而不再介绍的其他内容。 Admittedly she says she doesn't mind if we complete it as it was our first proper java lesson today. 诚然,她说她不介意我们是否完成它,因为这是我们今天的第一堂适当的Java课。

So in light of that, can anyone give me any tips on how to do this? 因此,鉴于此,有人可以给我有关如何执行此操作的提示吗? I don't want a complete piece of code to do exactly that as I need to learn and so a guiding hand instead would be greatly appreciated. 我不希望有完整的代码完全按照我需要学习的方式进行操作,因此,不胜感激。 What I have written so far: 到目前为止我写的是:

import java.util.*;
public class main
{
 /**
 * Constructor for objects of class main
 */
public static void main(String [ ] args)
{
    Scanner keyboard = new Scanner(System.in);
    String word = "";
    System.out.print("\nPlease enter a word: ");
    word = keyboard.nextLine();
    for(int i=0; i<word.length(); i++)
    {

    }
}

There's pretty much nothing there I know, but as mentioned we haven't exactly learnt anything yet so that's all I've managed to get in so far, and to be honest there's probably something wrong there already. 我几乎没有什么知道的,但是如前所述,我们还没有完全学到任何东西,所以到目前为止,我已经设法了解了所有这些信息,老实说,那里可能已经出了问题。

My assumption is there's a way for me to convert each letter to it's number (maybe the numbers are already assigned to letters in Java?) in a for loop. 我的假设是,我有一种方法可以在for循环中将每个字母转换为其数字(也许数字已经在Java中分配给字母了?)。 My only other idea is to convert it to an array, have another array with the numbers in and find a way to output the relevant counterpart. 我唯一的另一个想法是将其转换为一个数组,使用另一个包含数字的数组,并找到一种输出相关副本的方法。 But I'm not sure. 但是我不确定。

Thanks! 谢谢!

Anthony 安东尼

You can subtract two char variables. 您可以减去两个char变量。 This gives the difference between their ASCII values. 这给出了它们的ASCII值之间的差异。

for(int i=0; i<word.length(); i++)
{
    char c = word.charAt(i);
    System.out.println(c - 'a' +1);
}

Suppose you've read a String and need a int[] array where every number corresponds to letter in from a = 1 to z = 26 : 假设您已经阅读了一个String并且需要一个int[]数组,其中每个数字都对应于a = 1z = 26字母:

public int[] toNumbers(String s) {
    if (s == null)
        return null;
    int[] r = new int[s.length()];
    int i = 0;
    for (char c : s.toLowerCase().toCharArray())
       r[i++] = c - 'a' + 1;
    return r;
}

To print result, just use Arrays.toString() : 要打印结果,只需使用Arrays.toString()

System.out.println(Arrays.toString(r));

Try to use parseInt for int variable & parseDouble for double variable for eg String num1 = "123"; /* <-- it's a string no a number */ String num2 = "2.3"; int x = Integer.parseInt(num1); /* String is converted to int */ int y = Double.parseDouble(num2); /* String is converted to double */ 尝试对int变量使用parseInt,对double变量使用parseDouble,例如String num1 = "123"; /* <-- it's a string no a number */ String num2 = "2.3"; int x = Integer.parseInt(num1); /* String is converted to int */ int y = Double.parseDouble(num2); /* String is converted to double */ String num1 = "123"; /* <-- it's a string no a number */ String num2 = "2.3"; int x = Integer.parseInt(num1); /* String is converted to int */ int y = Double.parseDouble(num2); /* String is converted to double */ String num1 = "123"; /* <-- it's a string no a number */ String num2 = "2.3"; int x = Integer.parseInt(num1); /* String is converted to int */ int y = Double.parseDouble(num2); /* String is converted to double */ . String num1 = "123"; /* <-- it's a string no a number */ String num2 = "2.3"; int x = Integer.parseInt(num1); /* String is converted to int */ int y = Double.parseDouble(num2); /* String is converted to double */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM