Java 中的三角形模式与 while 循环

Triangle pattern in Java with while loop

你好,我的任务是制作一个三角形生成器,其中包含用户在 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;
  }

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

你知道我怎么用这个逻辑来解决吗

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

如果输入是 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
*
=====================
    *
   **
  ***
 ****
*****

你只需要一个Scanner,如果你使用String.repeat(int),算法会容易得多。先重复spaceinp - 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(str2.repeat(c));
    c++;
}

示例输出

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

我无法编辑@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
#
=====================
          #
         ##
        ###
       ####
      #####
     ######
    #######
   ########
  #########
 ##########

谢谢,希望对您有所帮助