2017-10-30 1 views
-2

私は現在、私のピラミッドの反対側を作成しています。私のプログラムでは、ユーザーに5〜15の数字を尋ねることができます。この数字を使用して、正方形と三角形を印刷します。私はピラミッドに着くまで、すべてを行うことができました。私はピラミッドの片面を作ることができますが、もう片面を作ることになると、私は何かを見落としていることに気付きました。私を正しい方向に向けるためのガイダンスは非常に高く評価されます。java printピラミッドネストループの反対側

import java.util.Scanner; 
public class doLoop { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Scanner input = new Scanner(System.in); 

     int number; 
     final int minimum = 5; 
     final int maximum = 15; 

     do { 
      System.out.print("Enter a number between" + " " + minimum + " " + "and" + " " + maximum + ":"); 
      number = input.nextInt(); 

      for(int i = 1; i <= number; i++) { 
       for(int j = 1; j <= number; j++) { 
        System.out.print(j + " "); 
       } 
       System.out.println(); 
      } 
      for(int column = 1; column <= number; column++) { 
       for(int row = 1; row <= number ; row++) { 
        if(column >= row) { 
         System.out.print(row); 
        } else { 
         System.out.print(" "); 
        } 
       } 

       System.out.println(" "); 
      } 

      if (number <= minimum || number >= 15) 
       System.out.println("Sorry, invalid"); 
     } while (number <= minimum || number >= maximum); 
    } 
} 


**Here is my current output:** 

Enter a number between 5 and 15:5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1  
12  
123 
1234 
12345 
Sorry, invalid 
Enter a number between 5 and 15: 

**This is what i'm working towards:** 

Enter a number between 5 and 15: 2 
Sorry, 2 is invalid. Please try again. 
Enter a number between 5 and 15: 20 
Sorry, 20 is invalid. Please try again. 
Enter a number between 5 and 15: 10 

1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 


          1 
         2 1 2 
         3 2 1 2 3 
        4 3 2 1 2 3 4 
       5 4 3 2 1 2 3 4 5 
      6 5 4 3 2 1 2 3 4 5 6 
      7 6 5 4 3 2 1 2 3 4 5 6 7 
     8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 
    9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 
+1

[このコードはなぜ動作しないのですか?]は、**目的の動作**、特定の問題またはエラー、**再現するために必要な最短コード**を含んでいる必要があります質問自体に。 (https://stackoverflow.com/help/on-topic)参照:[最小限で完全で検証可能なサンプルを作成する方法](https:// stackoverflow .com/help/on-topic)。 – Turing85

+1

ようこそ。あなたの質問に答えるかもしれない人々の靴に身を置いてください。正方形と三角形?出力がどのようになっているかを少なくとも表示してください –

+0

"フルピラミッドを印刷する" - 私はあなたが表示したいアスキーアに入れて、すべてが100になるように "ピラミッドを印刷"できる方法を想像することができます%clear –

答えて

0

コードに複数のエラーがあります!

  1. あなたのコードは、ループ永遠
  2. 私はcorrerctly理解していれば、あなたの問題はあなたがピラミッドの左側をしたいということですdo loop
  3. を必要としないでしょう。否定的なユーザー入力からユーザーが挿入した値にループすることで実現できます

ここではコードが抜かれていますので、必要に応じて調整が必要です。

import java.util.Scanner; 

public class doLoop { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     int number; 
     final int minimum = 5; 
     final int maximum = 15; 

     System.out.print("Enter a number between" + " " + 
       minimum + " " + "and" + " " + maximum + ":"); 
     number = input.nextInt(); 

     if (number <= minimum || number >= 15) { 
      System.out.println("Sorry, invalid"); 
      return; 
     } 

     for (int i = 1; i <= number; i++) { 
      for (int j = 1; j <= number; j++) { 
       System.out.print(j + " "); 
      } 
      System.out.println(); 
     } 

     for (int row = 1; row < number; row++) { 

      for (int column = -(number - 1); column <= number; column++) { 
       int absValue = Math.abs(column); 
       // you need to use the absolute value 
       // to print the positive value and to perform column checks 
       if (absValue <= row) 
        System.out.print(absValue); 
       else { 
       // if the absolute value is greater the the current print 1 or 2 spaces 
       // based on the value of the column 
       //(2 spaces if lower then 10 otherwise 1 space only) 
        System.out.print(absValue < 10 ? " " : " "); 
       } 
       // If the absolute value of column is -1 or 1 you need to change 
       // the value to "1" to bypass the printing of 101 
       if (absValue == 1) 
       { 
        column = 1; 
       } 
      } 
      System.out.println(); 
     } 
    } 

} 
0

あなたがしたことをコピーするのではなく、自分が働いていたものを編集したいと思います。さらに、私はあなたのコードのすべてを理解していません。私が探していた三角形を完成させることができました。しかし何らかの理由で私のループは私が設定した条件に従うことができません。私は、形状のforループを追加する前に、最初は働いていましたが、今度は、それらを再びフォローする方法について固執しています。何かアドバイス?

import java.util.Scanner; 
public class doLoop { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    Scanner input = new Scanner(System.in); 

    int number; 
    final int minimum = 5; 
    final int maximum = 15; 




    do { 
     System.out.print("Enter a number between" + " " + minimum + " " + "and" + " " + maximum + ":"); 
     number = input.nextInt(); 

    if (number <= minimum || number >= maximum) { 
     System.out.println("Sorry, invalid"); 
    break; 
} 
       for(int i = 1; i <= number; i++) { 

     for(int j = 1; j <= number; j++) { 
      System.out.print(j + " "); 

     } 
     System.out.println(); 

     } 
       int columns = 1; 

       for (int i = number; i >= 1; i--) 
       { 
        for (int j = 1; j <= i*2; j++) 
        { 
         System.out.print(" "); 

       } 
       for (int j = i; j <= number; j++) 
       { 
        System.out.print(j + " "); 
       } 

       for (int j = number - 1; j >= i; j--) 
       { 
        System.out.printf(j + " "); 
       } 

        System.out.println(); 

        columns++; 
       } 





    } while (number <= minimum || number >= maximum); 




} 


} 
関連する問題