我刚学Java,如果这个问题很显然,那么请原谅。
我正在尝试逐个字符读取字符串以创建树节点。例如,输入"HJIOADH"
此时节点为H J I O A D H
我注意到
char node = reader.next().charAt(0); I can get the first char H by this
char node = reader.next().charAt(1); I can get the second char J by this
我可以使用循环来获取所有的字符吗?例如:
for i to n
node = reader.next().charAt(i)
我尝试了但它不起作用。
我应该怎么做?
非常感谢任何帮助。
Scanner reader = new Scanner(System.in); System.out.println("输入您的节点,以大写字母形式输入,末尾不带空格和'/'符号"); int i = 0; char node = reader.next().charAt(i); while (node != '/') {
CreateNode(node); // this is a function to create a tree node
i++;
node = reader.next().charAt(i);
}
char[] array = "HJIOADH".toCharArray();
获取数组中的字符。查看String
类本身中存在的方法。 - Braj