我想用星星(*)打印'8'图案。我尝试过的代码在下面显示错误的输出。 [英] I want to print '8' pattern using the stars(*). My tried code is below which is showing wrong output.

查看:83
本文介绍了我想用星星(*)打印'8'图案。我尝试过的代码在下面显示错误的输出。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Stars 
{
	public static void main(String[] args) 
	{
		int n= 5;
		for(int i=0;i<=n;i++)
		{
			for(int j=0;j<=n;j++)
			{
				if(i==0||i==n||i==n/2||j==0||j==n)
				{
				System.out.print("*   ");
				}
			}
				
			System.out.println(" ");
			System.out.println(" ");
		
	}
}

}

[edit]已添加代码块 - OriginalGriff [/ edit]



我尝试了什么:



我搜索了谷歌但找不到解决方案。

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

I have searched google but could not find solution.

推荐答案

你忘了一个 else ...

You forgot an else...
class Stars
{
  public static void main(String[] args)
  {
    int n= 5;
    for(int i=0;i<=n;i++)
    {
      for(int j=0;j<=n;j++)
      {
        if(i==0||i==n||i==n/2||j==0||j==n)
          System.out.print("*");
        else
          System.out.print(" ");

      }
      System.out.println(" ");
      System.out.println(" ");
    }
  }
}


你不会在互联网上找到解决方案,因为这是你的作业,你应该自己做。



看看你的代码:为什么有两个 println 命令连续?

为什么你认为你应该打印的唯一的另一件事就是一个明星,然后是三个空格?



看看你需要打印什么:

You won't "find solution" on the internet, becuase this is your homework, and you are expected to do it yourself.

Look at your code: Why are there two println commands in a row?
Why do you think that the only other thing you should print is a star followed by three spaces?

Look at what you need to print:
*****
*   * 
*****
*   *
*****

所以每次你的循环,你应该打印'*'或''

开始扔掉你的身体内循环,并考虑你在做什么。

线号是奇数还是偶数?如果它很奇怪,你总是打印一颗星。

如果不是,你打印一个空格,除非它是打印空格时的第一列或最后一列。

在内循环之后,打印一个空行。

So each time round your loop, you should either be printing a '*' or a ' '
Start by throwing away the body of your inner loop, and thinking about what you are doing.
Is the line number odd or even? If it's odd, you always print a star.
if it isn't, you print a space, unless its the first or last column when you print a space.
After the inner loop, you print one blank line.


这篇关于我想用星星(*)打印'8'图案。我尝试过的代码在下面显示错误的输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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