【问题标题】:Triangle pattern in Java with while loopJava中的三角形模式与while循环
【发布时间】:2022-08-21 03:37:18
【问题描述】:

您好,我的任务是制作一个三角形生成器,其中包含用户在 java 中想要的符号和行数,使用 while 循环它就像这样工作。


*
**
***
****
*****
******
*******
********

但我想要这样

       *
      **
     ***
    ****
   *****
  ******
 *******
********

这是我的代码

import java.util.Scanner;
import java.lang.Math;

class Main {
 public static void main(String args[]){
  
  ////////////OBJECTS///////////////////////

  Scanner sc = new Scanner(System.in);
  Scanner sc2 = new Scanner(System.in);
  ///////////////////////INPUTS//////////

  System.out.println(\"Please Enter how many rows do you want\");
  int inp = sc.nextInt();
  
  System.out.println(\"Which symbol do you want\");
  String str2 = sc2.nextLine();
 ////////////////VARIABLES///////////////////

  int c = 0; 
  String str = \"\";
  String str3 = \"\";
  System.out.println(\"=====================\");

  ///////LOGIC/////////////////////////////

  while(c != inp){
   str = str+str2; 
   System.out.println(str);
   c=c+1;
  }

////////////////////////////////////////////
  
 }
 
}

你知道我怎么能用这个逻辑解决它

    标签: java design-patterns while-loop


    【解决方案1】:

    你只需要一个Scanner,如果你使用String.repeat(int),算法会容易得多。首先重复空格inp - c 次,然后重复你的“符号”c 次。喜欢,

    Scanner sc = new Scanner(System.in);
    System.out.println("Please Enter how many rows do you want");
    int inp = sc.nextInt();
    sc.nextLine();
    System.out.println("Which symbol do you want");
    String symbol = sc.nextLine(); // You had a good name in the prompt.
    int c = 1;
    System.out.println("=====================");
    while (c <= inp) {
        System.out.print(" ".repeat(inp - c));
        System.out.println(symbol.repeat(c));
        c++;
    }
    

    示例输出

    Please Enter how many rows do you want
    5
    Which symbol do you want
    *
    =====================
        *
       **
      ***
     ****
    *****
    

    【讨论】:

      【解决方案2】:

      对于模式的每一行,您可以在空格和您尝试打印的字符之间形成一个模式。

      如果输入为 5,您可以看到空格在减少(第一行 4,第二行 3)并且字符在增加(第一行 1,第二行 2)以及空格数和字符的总和( *) 是常数(等于 5) 因此,对于每一行,您必须多次打印空格和字符。

      此外,您不需要 2 个扫描仪对象来读取输入。读取整数后,可以使用scan.next()读取最后一个字符(why?)

      class Main {
          public static void main(String args[]) {
      
              //////////// OBJECTS///////////////////////
      
              Scanner sc = new Scanner(System.in);
              // Scanner sc2 = new Scanner(System.in);
              /////////////////////// INPUTS//////////
      
              System.out.println("Please Enter how many rows do you want");
              int inp = sc.nextInt();
              sc.nextLine();
              // sc.next();
              System.out.println("Which symbol do you want");
              String str2 = sc.nextLine();
              //////////////// VARIABLES///////////////////
      
              int c = 1;
              String str = "";
              String str3 = "";
              System.out.println("=====================");
      
              /////// LOGIC/////////////////////////////
      
               int i = 1;
          int j = 0;
          while (i <= inp) {
              while (j < inp - i) {
                  System.out.print(" ");
                  j++;
              }
              while (j < inp) {
                  System.out.print(str2);
                  j++;
              }
              System.out.println();
              j = 0;
              i++;
          }
      
              sc.close();
              ////////////////////////////////////////////
      
          }
      
      }
      

      输出是

      Please Enter how many rows do you want
      5
      Which symbol do you want
      *
      =====================
          *
         **
        ***
       ****
      *****
      

      【讨论】:

      • 谢谢,但我需要有关 while 循环的帮助
      • @没有人编辑过它并使用了 while 循环
      【解决方案3】:

      我无法编辑@Elliott Frisch 的答案。这就是我给出这个答案的原因。您只需额外添加 3 行即可执行您想要的操作,并且无需删除您的 Scanner 对象

      首先导入 java.io.*

      import java.io.*;
      

      在变量中,您必须添加此行。

      String str3 = " ";
      

      在逻辑部分的 while 循环中,您必须替换此代码。

      System.out.println(str);
      

      对此;

      System.out.println(str3.repeat(inp)+str2);
      

      这是您编辑后的全部代码。

      import java.util.Scanner;
      import java.io.*;
      
      class Main {
       public static void main(String args[]){
        
        ////////////OBJECTS///////////////////////
      
        Scanner sc = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        ///////////////////////INPUTS//////////
        System.out.println("Please Enter how many rows do you want");
        int inp = sc.nextInt();
        System.out.println("Which symbol do you want");
        String str2 = sc2.nextLine();
       ////////////////VARIABLES///////////////////
       
        String str = str2;
        String str3 = " ";
        System.out.println("=====================");
      
        ///////LOGIC/////////////////////////////
      while(inp != 0){
       
       System.out.println(str3.repeat(inp)+str2);
       str2 = str+str2; 
       inp=inp-1; 
      }
      
      /////////////////END///////////////////////////
       }
       
      }
      
      

      这是输出

      Please Enter how many rows do you want
      10
      Which symbol do you want
      #
      =====================
                #
               ##
              ###
             ####
            #####
           ######
          #######
         ########
        #########
       ##########
      
      

      谢谢,希望对您有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 1970-01-01
        • 2011-11-04
        • 2019-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多