我有以下代码需要从用户那里获取 10 个字符并以相反的顺序打印出来。我似乎无法克服Scanner
. 如何一次输入一个字符?这是我到目前为止所拥有的:
import java.util.Scanner;
public class ReverseOrder
{
//-----------------------------------------------------------------
// Reads a list of char from user and prints in reverse.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
char[] letters = new char[10];
System.out.println ("The size of the array: " + letters.length);
for (int index = 0; index < letters.length; index++)
{
System.out.print ("Enter number " + (index+1) + ": ");
letters[index] = scan.nextchar(); //doesnt like this line
}
System.out.println ("The numbers in reverse order:");
for (int index = letters.length-1; index >= 0; index--)
System.out.print (letters[index] + " ");
}
}