我有以下代碼需要從用戶處取出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] + " ");
}
}
此外,見http://stackoverflow.com/questions/18746185/why-doesnt-the-scanner-class-have -a-nextchar-method和http://stackoverflow.com/questions/2597841/scanner-method-to-get-a-char和http://stackoverflow.com/questions/19417813/compiler-says-java-code -is-invalid/19417824#19417824 – Justin