C#.net代码错误请参阅以下代码并纠正错误 [英] C# .net code error please see the following code and rectify the Error

查看:96
本文介绍了C#.net代码错误请参阅以下代码并纠正错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends,



我在c#语言的帮助下练习编程工作。

我发现这个程序无法打印所需的输出我想要的。



请帮我找出错误。



谢谢你。



代码。

...................... ......

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 logical_star_outer_pyramid
{
class 计划
{
静态 void Main( string [] args)
{
Console.WriteLine( 输入你的号码);
int i = int .Parse(Console.ReadLine());
for int j = 0 ; j < = i; j ++)
{
for int k = 1 ; k < =(i - j); k ++)
Console.Write( );
for int l = 1 ; l < = j; l ++)
{
if ( l == 1 || l == j || i == j)
Console.Write( * + );
else
Console.Write( );
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}





输出应该是这样的: -

输入数字: -

4; IE;外金字塔(内金字塔或星星不应出现)



<前lang =text> *
* *
* *
* * * * *

解决方案

为什么使用3个循环?

  for  int  j =  0 ; j <  = i; j ++)
{
for int k = 1 ; k < =(i - j); k ++)
Console.Write( );
for int l = 1 ; l < = j; l ++)





你只需要使用2个循环!想象一下,你需要填充数组...

伪代码:

  string  stars =  string  .Empty; 
for int i = 1 ; i< = NumberEnteredByUser; i ++)
{
for (j = 1 ; j< = i; j ++)
{
// concat *
stars = string .Concat(stars, *);
}
// print
Console.WriteLine(星星) ;
// clear string
stars = 字符串 .Empty;
}





知道了吗?



注意字符串是不可改变的。这是什么意思?在这里阅读:为什么字符串是不可变的,有什么影响它是什么? [ ^ ]


Hello Friends,

I am practicing my programing assignment with the help of c# language.
I found that this program unable to print the desired output what i want.

Please help me out in finding the error.

Thank you.

Code.
............................

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace logical_star_outer_pyramid
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your Number");
            int i = int.Parse(Console.ReadLine());
            for (int j = 0; j <= i; j++)
            {
                for (int k = 1; k <= (i - j); k++)
                    Console.Write(" ");
                for (int l = 1; l <= j; l++)
                {
                    if (l == 1 || l == j || i == j)
                        Console.Write("*" + "");
                    else
                        Console.Write(" ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}



output should be like:-
Enter the Number:-
4;I.E; Outer Pyramid (inner pyramid or stars should not present)

    *
  *   *
 *     *
* * * * *

解决方案

Why do you use 3 loops?

for (int j = 0; j <= i; j++)
{
    for (int k = 1; k <= (i - j); k++)
        Console.Write(" ");
    for (int l = 1; l <= j; l++)



You need to use only 2 loops! Imagine, you need to fill array...
In pseudo-code:

string stars = string.Empty;
for (int i = 1; i<=NumberEnteredByUser; i++)
{
    for (j = 1; j<=i; j++)
    {
        //concat *
        stars = string.Concat(stars, "*");
    }
    //print
    Console.WriteLine(stars);
    //clear string
    stars = string.Empty;
}



Got it?

Note that string is immutable. What it means? Read here: Why strings are immutable and what are the implications of it?[^]


这篇关于C#.net代码错误请参阅以下代码并纠正错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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