当前位置: 首页 > 面试题库 >

java打印一个三角形

田嘉澍
2023-03-14
问题内容

我正在尝试制作一个程序,该程序需要用户输入诸如三角形应该多长时间及其方向。我的问题是,在我运行该程序后,它会继续向程序中添加更多的数字。

例如

State the length of the two sides (finish with -1):  5
Should the triangle face down (0) or  up(1): 1
*
**
***
****
*****

2
Should the triangle face down (0) or  up(1): 1
*
**
***
****
*****
******
*******

我的代码:

import java.util.Scanner;

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

        // Initierings variabler för triangelsida.
        double length = 0;
        double sideLength = 0;

        // This part will ask for user input
        System.out
                .print("State the length of the two sides (finish with -1): ");

        while (sideLength != -1) {
            // Input.
            sideLength = in.nextDouble();
            if (sideLength != -1) {
                // Input will be saved in variable length.
                length += sideLength;

                // This part will ask the user to state whether the triangle is
                // up or down.
                System.out
                        .print("Should the triangle face down (0) or  up(1): ");
                String direction = in.next();

                // if the variables direction is equal to (1) this part will
                // run.
                if (direction.equals("1")) {
                    for (int i = 1; i <= ((int) (length)); i++) {
                        for (int j = 1; j <= i; j++) {
                            System.out.print("*");
                        }
                        System.out.println();
                    }

                }
                // if direction equals to (0) .
                else {
                    for (int i = 1; i <= ((int) (length)); i++) {
                        for (int j = ((int) (length)); j >= 1; j--) {
                            if (j >= i)
                                System.out.print("*");
                        }
                        System.out.println();
                    }
                }

            }

        }

    }

}

问题答案:

你有length += sideLength。对于while循环的每个循环,这将继续将sideLength输入添加到length变量中。您可能想要的只是length = sideLength

要使其在每次迭代中再次打印出您的第一个提示,只需将您的System.out.print("State the length of the two sides (finish with -1): ");调用放入while循环内即可。(它也必须先出现sideLength = in.nextDouble();,以便 输入输入 之前 显示提示。)



 类似资料:
  • 所以我得到了这个目标:我想让用户输入三角形的线数。然后我想打印三角形只使用3个字母:a、b、C。 我试过这段代码: 但这不起作用,任何向导?

  • 问题内容: 目标:我需要画一个直角三角形。 我收到此结果: 我想画这个形状: 我的代码是: 问题答案: 这是python 3.x版本: 注: 在 结束=“” 保证打印时没有紧跟一个新行。

  • 问题内容: 我需要打印一个三角形及其倒三角形(位于其尖端)。我设法只打印出三角形。我知道我可以轻松地使用for循环,但是我想知道如何使用递归,在我的情况下,我不知道如何同时打印三角形和倒置三角形,谢谢。 我的代码: 我的结果: 问题答案: 您必须重新考虑问题,这可能是一种可能的解决方案:

  • 本文向大家介绍打印出杨辉三角形(要求打印出10行如下图)。相关面试题,主要包含被问及打印出杨辉三角形(要求打印出10行如下图)。时的应答技巧和注意事项,需要的朋友参考一下 【参考答案】  

  • 问题内容: 嗨,我正在尝试创建仿射变换,使我可以将一个三角形变换为另一个三角形。我所拥有的是2个三角形的坐标。你能帮助我吗? 按照亚当·罗森菲尔德的回答,我想出了这段代码,以防万一有人无聊地自己解决方程: 问题答案: 我假设您在这里谈论2D。仿射变换矩阵中有9个值: 有3个顶点输入,和,当其转化应该成为,,。然而,由于我们在齐次坐标的工作,应用到不一定给-它给人的倍数。所以,我们也有未知的乘法器,

  • 本文向大家介绍python 打印直角三角形,等边三角形,菱形,正方形的代码,包括了python 打印直角三角形,等边三角形,菱形,正方形的代码的使用技巧和注意事项,需要的朋友参考一下 三角形 等腰直角三角形1 2.7 python:打印直角三角形 coding=utf-8 方式一 方式二 #打印实心等边三角形 #打印菱形 #实心正方形 #空心正方形 知识点说明: python ,end=''备注