为什么我的java程序不能正常循环?

时间:2014-07-30 05:20:39

标签: java

以下程序应打印选项屏幕,要求用户选择S(方形),H(形状),T(三角形)或X(形状)等选项。

它会根据要求用户输入的尺寸和字符打印该形状。它是一个循环并保持循环和打印形状,直到用户选择最后一个选项Q(退出)。一旦用户输入Q,它将结束。

这个程序第一次工作并打印一个形状。但是当它再次循环时,它只打印到“displayMenu()”的末尾。然后显示错误说:

  

输入您的选项:线程“main”中的异常   java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:   0


这是程序..请帮我找出循环不起作用的原因。

import java.util.Scanner;
public class Shapes{
   public static void main(String[] args){
      Scanner keyboard = new Scanner(System.in);
      int size = 0;
      char ch = ' ', option = ' ';
      boolean done = false;
      displayWelcome();

      do{
         displayMenu();
         option = getOption(keyboard);
         if(option == 'Q')
            done = true;
         else{
            ch = getCharToPrint(keyboard);
            size = getSize(keyboard);
            if(option == 'S')
               printSquare(size, ch);
            else if(option == 'H')
               printHShape(size, ch);
            else if(option == 'T')
               printTriangle(size, ch);
            else
               printXShape(size, ch);

               System.out.println();
         }
      } while(!done);

      System.out.print("Done");
   }

   public static void displayWelcome(){
      System.out.println("WELCOME TO THE SHAPE PRINTER!");
      System.out.println("-----------------------------");
   }

   public static void displayMenu(){
      System.out.println("  Options:  ");
      System.out.println("    S)quare  ");
      System.out.println("    H) Shape  ");
      System.out.println("    T)riangle  ");
      System.out.println("    X) Shape  ");
      System.out.println("    Q)uit  ");
      System.out.println("-------------------------------");

   }

   public static char getOption(Scanner keyboard){
      char answer;
      String input;
      do{
         System.out.print("Enter your option: ");
         input = keyboard.nextLine();
         answer = input.charAt(0);
   } while (answer != 'S' && answer != 'H' && answer != 'T' && answer != 'X' && answer != 'Q');
   return answer;
   }

   public static char getCharToPrint(Scanner keyboard){
      char ch;
      String input;
      System.out.print("Enter a character: ");
      input = keyboard.nextLine();
      ch = input.charAt(0);
      return ch;
   }

   public static int getSize(Scanner keyboard){
      int size;
      do{
         System.out.print("Enter an odd, positive number: ");
         size = keyboard.nextInt();
      } while(size % 2 == 0 || size < 1);
      return size;   
   }

   public static void printSquare(int n, char ch){
      int row, col;
      for(row = 0; row < n; row++){
         for (col = 0; col < n; col++){
            if(row == 0 || row == n-1 || col == 0 || col == n-1)
               System.out.print(ch);
            else 
               System.out.print(' '); 
         }
         System.out.println();
      }
   }

   public static void printHShape(int n, char ch){
      int row, col, mid = n/2;
      for(row = 0; row < n; row++){
         for(col = 0; col < n; col++){
            if(col == 0 || col == n - 1 || row == mid)
               System.out.print(ch);
            else
               System.out.print(' ');
         }
         System.out.println();
      }
   }

   public static void printTriangle(int n, char ch){
      int row, col;
      for(row = 0; row < n; row++){
         for (col = 0; col < n; col++){
            if(col == 0 || row == n - 1 || row == col)
               System.out.print(ch);
            else
               System.out.print(' ');
         }
         System.out.println();
      }
   }

   public static void printXShape(int n, char ch){
      int row, col, mid = n/2;
      for(row = 0; row < n; row++){
         for (col = 0; col < n; col++){
            if(row == col || row + col == n - 1)
               System.out.print(ch);
            else
               System.out.print(' ');
         }
         System.out.println();
      }
   }
}

1 个答案:

答案 0 :(得分:4)

<强>问题:

size = keyboard.nextInt();

您正在从用户那里获得一个新的行符号,然后在第二个循环中按nextLine()消耗,从而将StringIndexOutOfBoundsException投放到此行answer = input.charAt(0);

<强>溶液

在循环到选项

之前首先使用换行符
System.out.print("Enter an odd, positive number: ");
     size = keyboard.nextInt();
     keyboard.nextLine();