为什么我在线程主错误中遇到异常?

时间:2013-11-22 00:10:27

标签: java multithreading exception main

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] + "  ");
       }

}

3 个答案:

答案 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