Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要做一个倒三角形_Java_For Loop_Methods - Fatal编程技术网

Java 我需要做一个倒三角形

Java 我需要做一个倒三角形,java,for-loop,methods,Java,For Loop,Methods,我需要写一个程序,给定一个数字和一个字母,它将打印字母的次数与给定的数字相同。然后,我需要调用第一个方法,使用给定的值来生成一个三角形。 第一个输出应该是这样的,如果我给它参数(5,“u”)=“uuu” 我已经有了第一部分,但是我需要调用第一个方法并获得如下输出: u uu uuu uuuu uuuuu 这是我到目前为止对代码的了解: public class Triangle { private String theLetter; private

我需要写一个程序,给定一个数字和一个字母,它将打印字母的次数与给定的数字相同。然后,我需要调用第一个方法,使用给定的值来生成一个三角形。 第一个输出应该是这样的,如果我给它参数(5,“u”)=“uuu” 我已经有了第一部分,但是我需要调用第一个方法并获得如下输出:

    u

   uu

  uuu

 uuuu

uuuuu
这是我到目前为止对代码的了解:

public class Triangle
{

    private String theLetter;
    private int cnt;
    private String aLetter;
    private int howMany;

    public void getLetters(int cnt, String theLetter)
    {
        System.out.print("\"");
        for(int x=0; x < cnt; x++)
        {
            System.out.print(theLetter);
        }
        System.out.print("\"");
        System.out.println();
    }

    public void getLetterTriangle(int howMany, String aLetter)
    {
        for(int i = 0; i < howMany ; i++)
        {
            getLetters(howMany, aLetter);
        }
            System.out.println();
    }
}
公共类三角形
{
私用线绳;
私人互联网;
私有字符串选择器;
私人int有多少;
public void getLetters(int-cnt,String-theLetter)
{
系统输出打印(“\”);
对于(int x=0;x

我就是不能让for循环来做这件事。请帮助。

您应该了解这一部分:

for(int i = 0; i < howMany ; i++)
{
    getLetters(howMany, aLetter);
}
for(int i=0;i
如果仔细观察,您会发现循环中不断变化的变量是
i
,而不是
howmounty
。因为这显然是一个家庭作业,我把其余的留给你


更新:您还应该了解Martijn Courtaux的答案。他对前面的空格有一个正确的观点。

您现在正在打印一个正方形。在
getLetters()
方法中,应该有两个部分。第一部分应打印空格,第二部分应打印字母。您的
getLetters()
方法需要一个额外的参数,以便在打印字母之前,它可以知道必须打印多少空格。

将getLetters中的代码更改为:

public void getLetters(int cnt, int howMany, String theLetter)
{
    System.out.print("\"");
    for(int x=0; x < (howMany-cnt); x++) // you need to display spaces
        System.out.print(" ");
    for(int x=0; x < cnt; x++)
        System.out.print(theLetter);
    System.out.print("\"");
    System.out.println();
}
public void getLetters(int cnt,int howmount,String theLetter)
{
系统输出打印(“\”);
对于(int x=0;x<(多少cnt);x++)//需要显示空格
系统输出打印(“”);
对于(int x=0;x
在这个三角形里

public void getLetterTriangle(int howMany, String aLetter)
{
    for(int i = 1; i <= howMany ; i++) // start with 1
        getLetters(i, howMany, aLetter); // display i, total width howMany
}
public void getletterangle(整数多少,字符串长度)
{

对于(int i=1;我请澄清问题是什么。问题是我不能做for循环来获得三角形的形状,但有人已经给我看了代码,但无论如何还是要感谢@Saposhiente-1:这是家庭作业。不要破坏。不要给出普通代码。至少要给出一个非常彻底的解释。@MartijnCourteaux我已经对e做了评论每改变一部分,他们就足以理解问题所在。谢谢@Lashane的帮助。我知道问题就在那里。我不知道如何进行for循环来完成。我已经尝试过了,但我无法做到。我甚至问过我的一些朋友,但仍然没有改变。我知道我需要自己去尝试,但有些事情我不知道不要理解,因为我们在课堂上看到的信息还不够。但无论如何,谢谢你。至少在你在课堂上展示拉桑的代码之前,试着理解它。实际上,你能做的最好的事情就是坐下来,一行一行地思考代码做了什么。你也可以使用调试器来跟踪真正的程序流和变量的值。