2017-10-13 1 views
-2

コーディングに問題があります。私が形状の高さと幅を入力するように求めるプログラムを作成しようとしています。私はJavaクラスを取っていると考えて、私は初心者です。アスタリスクの2つの平行な形が必要であり、正方形または長方形であることができる。Java Homeworkトラブルアスタリスク

ありがとうございます!

私がこれまでにちょっと

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されているコード私は、彼らがのを継続した場合にあなたを教えているかわかりません。私はあなたがfalseに設定していないので、while(true)を使って無限ループを作ったと思います。 先週の教訓や、少なくともあなたが必要とするものに変更されました。

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を処理してプログラムを終了させないようにする
    関連する問題