【发布时间】:2014-09-23 07:22:49
【问题描述】:
我正在尝试将我的第一个程序文本转换为 ASCII 转换器,但我遇到了一些问题,代码中对此进行了解释:
import java.util.Scanner;
public class AsciiConverter {
public static void main(String[] args){
System.out.println("Write some text here");
Scanner scanner = new Scanner(System.in).useDelimiter("\\n"); // Scans whole text
String myChars = scanner.next();
int lenght = myChars.length(); // Checking length of text to use it as "while" ending value
int i = -1;
int j = 0;
do{
String convert = myChars.substring(i++,j++); // taking first char, should be (0,1)...(1,2)... etc
int ascii = ('convert'/1); // I'm trying to do this, so it will show ascii code instead of letter, error: invalid character constant
System.out.print(ascii); // Should convert all text to ascii symbols
}
while(j < lenght );
scanner.close();
}
}
【问题讨论】:
-
为什么你在
' '中使用'convert'?它是变量的名称。写int ascii = (convert/1); -
被
int ascii = ('convert'/1);这行搞糊涂了,那该怎么办? -
获取 ASCII 码作为结果的技巧 :)
-
charAt将在字符串中的给定位置返回char值。char是一个 16 位无符号数字,它是字符的数字代码。因此,System.out.println((int) myString.charAt(index));将在index处打印字符的数值。 -
int ascii = ('convert'/1);在 java 8 中无法编译
标签: java