import java.util.Scanner;
public class ReverseOrder
{
char input;
public static void main (String[] args)
{
Scanner reader = new Scanner (System.in);
char [] ch = new char[5];
System.out.println ("The size of the array: " + ch.length);
for (char index = 0; index < ch.length; index++)
{
System.out.print ("Enter a char " + (index+1) + ": ");
ch[index] = reader.next().charAt(0);
}
System.out.println ("The numbers in reverse order:");
for (char index = (char) (ch.length-1); index >= 0; index--)
System.out.print (ch[index] + " ");
}
}
答案 0 :(得分:0)
您遇到char
溢出。
主要问题在于你的循环......
for (char index = 0; index < ch.length; index++)
和
for (char index = (char) (ch.length-1); index >= 0; index--)
哪些人正在使用char
。请尝试将其更改为使用int
,例如......
for (int index = 0; index < ch.length; index++)
和
for (int index = (ch.length-1); index >= 0; index--)
答案 1 :(得分:0)
问题在于您char
for loops
更改为
for (int index = 0; index < ch.length; index++)
和
for (int index = ch.length - 1; index >= 0; index--)
答案 2 :(得分:0)
打印char数组时。
您应该进行以下更改:
更改
for (char index = (char) (ch.length-1); index >= 0; index--)
到
for (int index = ch.length-1; index >= 0; index--)
打印示例如下:
The size of the array: 5
Enter a char 1: 1
Enter a char 2: 2
Enter a char 3: 3
Enter a char 4: 4
Enter a char 5: 5
The numbers in reverse order:
5 4 3 2 1