简体   繁体   English

在Java中将字母表示为数字

[英]Representing letters as numbers in java

I was wondering how you would represent letters as integers in java. 我想知道如何在Java中将字母表示为整数。 I am working on a problem where I have to find the mid letter between a two lettered word. 我正在研究一个必须在两个字母的单词之间找到中间字母的问题。 For example, I would choose the word 'go' and provide each letter with an assigned integer value to find the midpoint letter. 例如,我将选择单词“ go”并为每个字母提供一个分配的整数值以查找中点字母。 Can anyone help me out with this or just point me in the right direction to go about solving on how to get the midpoint letter between a two letter word? 任何人都可以帮我解决这个问题,或者只是指出正确的方向去解决如何获得两个字母之间的中点字母吗?

That is simple 那很简单

    int a = 'a';
    int c = 'c';
    char mid = (char) ((a + c) / 2);
    System.out.println(mid);

prints 版画

b

(int)str.charAt(i) will get you an integer value (the ASCII value). (int)str.charAt(i)将为您提供一个整数值(ASCII值)。 For "regular" letters, this should allow you to do what you want. 对于“常规”字母,这应该允许您执行所需的操作。

str = "GO";
midLetter = Character.toChars(((int)str.charAt(0) + (int)str.charAt(1))/2);

I think I got the brackets to match... 我想我有匹配的括号...

If by "letters" you're referring to the char primitive type, then they are already represented by integers behind the scenes. 如果通过“字母”来指代char基本类型,那么它们已经由背景后面的整数表示。 From the Java Tutorials : Java教程

The char data type is a single 16-bit Unicode character. char数据类型是单个16位Unicode字符。 It has a minimum value of '\' (or 0) and a maximum value of '\￿' (or 65,535 inclusive). 它的最小值为'\' (或0),最大值为'\￿' (或65,535(含))。

So you can assign a char literal to an int variable for example: 因此,您可以将一个char文字分配给一个int变量,例如:

int g = 'g';
int o = 'o';

That should be enough to get you started. 那应该足以让您入门。

In java (and most other language) characters are actually represented as numbers. 在Java(和大多数其他语言)中,字符实际上表示为数字。 Google for 'ascii table', and you'll find out lowercase a is actually 97. Google的“ ascii table”,您会发现小写字母a实际上是97。

Assuming you want to index lowercase a as 0, then given arbitrary character from a string, you can subtract it with the 'a' chacater, and you will get the index 假设您想将小写字母a索引为0,然后从字符串中给定任意字符,则可以用'a'字符减去它,您将获得索引

String str = ...;
for(int i=0; i<str.length(); i++) {
   char c = str.charAt(i);
   int cIndex = c - 'a';

   // do something with cIndex...
}

If the given letter is of char then convert the type (int)yourword. 如果给定字母为char,则转换类型(int)您的单词。 Then find the midpoint 然后找到中点

Character.getNumericValue returns a numeric value for each character. Character.getNumericValue返回每个字符的数字值。 So, each letter has a unique numeric value via the Character class. 因此,每个字符都通过Character类具有唯一的数值。

This could be the starting point for your ordering, although you might need to consider case, etc. 这可能是订购的起点,尽管您可能需要考虑大小写等。

You should be able to go back and forth from int to char and perform arithmetic on your char values: 您应该能够从intchar往返,并对char值执行算术运算:

char median = 0;
for(char ch=65; ch<91; ch++) {
    System.out.println(ch);
    median += ch;
}
median = (char)(median/26);
System.out.println("=====");
System.out.println("Median leter in the alphabet: "+median);

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

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