2017-10-13 91 views
-2

我遇到了编码问题。我试图做一个程序,要求用户输入形状的高度和宽度。考虑到我正在上java课,我是一个新手。需要有两个平行形状的星号,可以是正方形或矩形。Java作业问题星号

谢谢!

代码我迄今在

import java.util.Scanner; 

public class rectangle { 

    public static void main(String... args) { 
     int recHeight = 0; 
     int recWidth = 0; 
     Scanner input = new Scanner(System.in); 

     do { 
      System.out.print("Enter height [-1 to quit] >> "); 
      recHeight = input.nextInt(); 

      if (recHeight == -1) { 
       System.exit(0); 
      } 

       /* check if number is valid */ 
      if (recHeight < 2 || recHeight > 24) { 
       System.err.println("--Error: please enter a valid number"); 
       continue; // prompt again 

       System.out.print("Enter width [-1 to quit] >> "); 
       recWidth = input.nextInt(); 

       if (recWidth == -1) { 
        System.exit(0); 
       } 

       /* check if number is valid */ 
       if (recWidth < 2 || recWidth > 24) { 
        System.err.println("--Error: please enter a valid number"); 
        continue; // prompt again 
       } 
       for (int col = 0; col < recHeight; col++) { 
        for (int row = 0; row < recWidth; row++) { 
         /* First or last row ? */ 
         if (row == 0 || row == recWidth - 1) { 
          System.out.print("*"); 
          if (row == recWidth - 1) { 
           System.out.println(); // border reached start a new line 
          } 
         } else { /* Last or first column ? */ 
          if (col == recHeight - 1 || col == 0) { 
           System.out.print("*"); 
           if (row == recWidth - 1) { 
            System.out.println(); 
           } 
          } else { 
           System.out.print(" "); 
           if (row == recWidth - 1) { 
            System.out.println(); 
           } 
          } 
         } 
        } 
       } 
      } 

     } while (true); 
    } 
} 
+1

你的问题是什么?你期待什么结果?你得到了什么? –

+0

[接受答案如何工作?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – shmosel

回答

1

是有点frankensteined我不知道他们教会了你用,如果继续的。我认为你创造了一个无止境的循环,同时(真),因为你从来没有把它设置为假。 以下是我们上周所教的内容,或者至少修改了您的需求。

import java.io.*; 
import java.util.Scanner; 

public class SquareDisplay 
{ 
    public static void main(String[] args) throws IOException 
    { 
     // scanner creation 
     Scanner stdin = new Scanner(System.in); 
     // get width 
     System.out.print("Enter an integer in the range of 1-24: "); 
     int side = stdin.nextInt(); 
     // check for < 1 or greather then 24 

     while((side < 1) || (side > 24)) 
     { 
      System.out.print("Enter an integer in the range of 1-24: "); 
      // reget the side 
      side = stdin.nextInt(); 
     } 
     // get height 
     System.out.print("Enter an integer in the range of 1-24: "); 
     int height = stdin.nextInt(); 
     // check for < 1 or greather then 24 

     while((height < 1) || (height > 24)) 
     { 
      System.out.print("Enter an integer in the range of 1-24: "); 
      // reget the height 
      height = stdin.nextInt(); 
     } 
     // create rows 
     for(int rows = 0; rows < side; rows++) 
     { 
      // creat cols 
      for(int cols = 0; cols < height; cols++) 
      { 
       System.out.print("X"); 
      } 
     System.out.println(); 
     } 
    } 

} 
+0

你会想用'try-with-resources扫描仪,就像一般的良好做法一样 – jrtapsell

0

下你想要做什么,并且还覆盖情况下,他们输入一个无效的号码,如example

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class SquareDisplay { 
    public static void main(final String... args) { 
     try (Scanner input = new Scanner(System.in)) { 
      final int columns = getInRange(input, 1, 24); 
      final int rows = getInRange(input, 1, 24); 
      for (int x = 0; x < columns; x++) { 
       for (int y = 0; y < rows; y++) { 
        System.out.print("X"); 
       } 
       System.out.println(); 
      } 
     } 
    } 

    private static int getInRange(final Scanner input, final int min, final int max) { 
     int returnValue = Integer.MIN_VALUE; 
     do { 
      System.out.printf("Please enter a value between %d and %d: ", min, max); 
      try { 
       returnValue = input.nextInt(); 
      } catch (final InputMismatchException ignored) { 
       // Ignore, keep asking 
      } 
      input.nextLine(); 
     } while (returnValue < min || returnValue > max); 
     return returnValue; 
    } 
} 

的优化

  • 使用try-with-resources处理资源管理
  • 提取读数以减少重复
  • 处理InputMismatchException停止查杀程序