在 Java 中将 *s 打印为三角形? [英] Printing *s as triangles in Java?

查看:15
本文介绍了在 Java 中将 *s 打印为三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 课程中的任务是制作 3 个三角形.一个左对齐,一个右对齐,一个居中.我必须为什么类型的三角形制作一个菜单,然后输入需要多少行.三角形必须看起来像这样

<预><代码>*****************************

到目前为止,我能够完成左对齐的三角形,但我似乎无法获得另外两个.我尝试谷歌搜索,但没有任何结果.任何人都可以帮忙吗?到目前为止,我有这个.

import java.util.*;公开课 Prog673A{public static void leftTriangle(){扫描仪输入 = 新扫描仪 (System.in);System.out.print("多少行:");int 行 = input.nextInt();for (int x = 1; x <= 行; x++){for (int i = 1; i <= x; i++){System.out.print("*");}System.out.println("");}}public static void rightTriangle(){扫描仪输入 = 新扫描仪 (System.in);System.out.print("多少行:");int 行 = input.nextInt();for (int x = 1; x <= 行; x++){for (int i = 1; i >= x; i--){System.out.print(" ");}System.out.println("*");}}public static void centerTriangle(){}public static void main (String args []){扫描仪输入 = 新扫描仪 (System.in);System.out.println("三角形的类型");System.out.println("	1.Left");System.out.println("	2.对了");System.out.println("	3.居中");System.out.print("请输入数字:");int menu = input.nextInt();如果(菜单== 1)左三角();如果(菜单== 2)直角三角形();如果(菜单== 3)centerTriangle();}}

样本输出:

三角形的类型1. 左2. 对3. 中心输入数字 (1-3):3多少行?:6************************************

解决方案

提示:对于每一行,你需要打印一些空格,然后然后 打印一些星星.每一行的空格数应该减少一个,而星星的数量应该增加.

对于居中的输出,每行增加两个的星数.

My assignment in my Java course is to make 3 triangles. One left aligned, one right aligned, and one centered. I have to make a menu for what type of triangle and then input how many rows is wanted. The triangles have to look like this

*
**
***
****


   *
  **
 ***
****


  *
 ***
*****

So far I was able to do the left aligned triangle but I can't seem to get the other two. I tried googling but nothing turned up. Can anyone help? I have this so far.

import java.util.*;
public class Prog673A
{
    public static void leftTriangle()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("How many rows: ");
        int rows = input.nextInt();
        for (int x = 1; x <= rows; x++)
        {
            for (int i = 1; i <= x; i++)
            {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
    public static void rightTriangle()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("How many rows: ");
        int rows = input.nextInt();
        for (int x = 1; x <= rows; x++)
        {
            for (int i = 1; i >= x; i--)
            {
                System.out.print(" ");
            }
            System.out.println("*");
        }
    }
    public static void centerTriangle()
    {

    }
    public static void main (String args [])
    {
        Scanner input = new Scanner (System.in);
        System.out.println("Types of Triangles");
        System.out.println("	1. Left");
        System.out.println("	2. Right");
        System.out.println("	3. Center");
        System.out.print("Enter a number: ");
        int menu = input.nextInt();
        if (menu == 1)
            leftTriangle();
        if (menu == 2)
            rightTriangle();
        if (menu == 3)
            centerTriangle();
    }
}

Sample Output:

Types of Triangles
1.  Left
2.  Right
3.  Center
Enter a number (1-3):  3
How many rows?: 6

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

解决方案

Hint: For each row, you need to first print some spaces and then print some stars. The number of spaces should decrease by one per row, while the number of stars should increase.

For the centered output, increase the number of stars by two for each row.

这篇关于在 Java 中将 *s 打印为三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆