使用 Java 创建用户输入星号三角形

我想要...

使用 Java创建一个星号三角形,它与用户输入的任何数字(1-50 之间)的长度相匹配。

细节

  1. 第一行总是以星号开头。

  2. 下一行将增加一个星号,直到它与用户的输入相匹配。

  3. 然后以下行将递减,直到它回到一个星号。

例如,如果用户输入 3,则输出将在第一行有一个星号,在第二行有两个星号,在第三行有三个星号,然后在结束前恢复为下一行的两个星号最后一行带有星号。

到目前为止我尝试过的

我需要使用嵌套的 for 循环。到目前为止,我尝试使用我在下面制作的这个练习示例对其进行测试。我只能在数字的输出上创建。我也有一些输出星号三角形的概念。如何应用此代码的概念来跟随用户输入的数字?

 import java.util.Scanner;


public class Program

{

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int count, index = 0, value, number;

        System.out.println("This program creates a pattern of numbers " );

        System.out.println("Based on a number you enter." );

            System.out.println("Please enter a positive integer. " );

        count = keyboard.nextInt();

        value = count;

        for (index = 1; index <= count; index++)

        {

            for (number = value; number >= 1; number--)

            {

                System.out.println(number);

            }

            value--;

            System.out.println();

        }

        }

}


料青山看我应如是
浏览 188回答 3
3回答

牧羊人nacy

这是我将如何进行编写一个printAsterisks将 int N 作为参数并写入一行 N 个星号的方法。您将需要一个for循环来执行此操作。printAsterisks在从 1 计数到 COUNT的for循环中调用调用printAsterisks从 COUNT-1 倒数到 1 的第二个循环这应该够了吧。另外,作为旁注,您应该关闭扫描仪。这样做的简单方法是将 ot 包含在 try-with-resource 中,如下所示:try (Scanner keyboard = new Scanner(System.in);) {&nbsp;// your code here}让我们知道可以运行的程序版本(或您仍然有的问题):)

牛魔王的故事

这是你想要的:public class Asterisk {&nbsp; &nbsp; private static final String ASTERISK = "*";&nbsp; &nbsp; private static final String SPACE = "";&nbsp; &nbsp; private static int LENGTH;&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readLength();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=1; i<=LENGTH; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == LENGTH) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j=LENGTH; j>=1; j--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawLine(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawLine(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You must enter a number between 1 and 50.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static void readLength(){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter asterisk's length (1-50)");&nbsp; &nbsp; &nbsp; &nbsp; LENGTH = Integer.parseInt(System.console().readLine());&nbsp; &nbsp; &nbsp; &nbsp; if (LENGTH<=0 || LENGTH>50)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NumberFormatException();&nbsp; &nbsp; }&nbsp; &nbsp; static void drawLine(int asterisks){&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder line = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; int spacesLeft = getLeftSpaceCount(asterisks);&nbsp; &nbsp; &nbsp; &nbsp; int spacesRight = getRightSpaceCount(asterisks);&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<spacesLeft; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line.append(SPACE);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<asterisks; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line.append(ASTERISK);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<spacesRight; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line.append(SPACE);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(line.toString()+"\n");&nbsp; &nbsp; }&nbsp; &nbsp; static int getLeftSpaceCount(int asterisks){&nbsp; &nbsp; &nbsp; &nbsp; int spaces = LENGTH - asterisks;&nbsp; &nbsp; &nbsp; &nbsp; int mod = spaces%2;&nbsp; &nbsp; &nbsp; &nbsp; return spaces/2 + mod;&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; static int getRightSpaceCount(int asterisks){&nbsp; &nbsp; &nbsp; &nbsp; int spaces = LENGTH - asterisks;&nbsp; &nbsp; &nbsp; &nbsp; return spaces/2;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}我需要使用嵌套的 for 循环是的,主要逻辑就在那里……for (int i=1; i<=LENGTH; i++) {&nbsp; &nbsp; if (i == LENGTH) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j=LENGTH; j>=1; j--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawLine(j);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; drawLine(i);}使用 5 作为输入的三角形。*************************小费:有一种更简单的方法可以使用System.console().readLine().

Cats萌萌

关于打印部分,我想稍微整理一下答案:int input = 3; //just an example, you can hook in user input I'm sure!for (int i = 1; i < (input * 2); i++) {&nbsp; &nbsp; int amount = i > input ? i / 2 : i;&nbsp; &nbsp; for (int a = 0; a < amount; a++)&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("*");&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}对于我们的循环条件,稍微解释一下:i < (input * 2): 因为i从 1 开始,我们可以考虑一些情况。如果我们输入 1,我们需要 1 行。输入 2、3 行。4:5行。简而言之,长度与行数的关系是row count = (length * 2) - 1,所以我从 1 而不是 0 开始另外偏移 1。i > input ? i / 2 : i:这称为三元语句,它基本上是一个 if 语句,您可以在其中获取表单中的值boolean/if ? value_if_true : value_if_false。因此,如果行数大于您请求的长度(超过一半),则长度除以 2。此外,该循环中的所有内容都可能是一行:System.out.println(new String(new char[i > input ? i / 2 : i]).replace('\0', '*'));是的,从技术上讲,使用 IntStream 我们可以将整个事情变成一条线,尽管那时我会为了清楚起见而换行请记住,我不会将其称为“初学者的解决方案”,但希望它可以激发您学习有关编程的其他一些有用的小知识,例如为什么我\0在单行示例中将其替换。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java