C# 用c语言制作金字塔#
我的问题是如何使用C#中的*和“空格”制作金字塔?输出将如下所示C# 用c语言制作金字塔#,c#,C#,我的问题是如何使用C#中的*和“空格”制作金字塔?输出将如下所示 * * * * * * * * * * * * * * * 我们只需要在这个程序中使用“for循环”。我只知道怎么做这个 * ** *** **** ***** 我制作了一个这样的程序: static void Main(string[]args) { int i=o; int j=o; for(i=5;1>=1;i--) for(j=1;j<=5;j++) { C
*
* *
* * *
* * * *
* * * * *
我们只需要在这个程序中使用“for循环”。我只知道怎么做这个
*
**
***
****
*****
我制作了一个这样的程序:
static void Main(string[]args)
{
int i=o;
int j=o;
for(i=5;1>=1;i--)
for(j=1;j<=5;j++)
{
Console.Write("*");
}
Console.WriteLine(" ");
}
static void Main(字符串[]args)
{
int i=o;
int j=o;
对于(i=5;1>=1;i--)
对于(j=1;j请考虑如何手动打印金字塔
假设有5层深
1st line: 4 spaces, 1 star,
2nd line: 3 spaces, star, space, star
3rd line: 2 spaces, star space star space star
等等
不管你是否在最后一颗星后打印空格,都不会改变它的外观
我们看到了什么
如果我们总共有X个级别
line 1: (x-1) spaces, (star space)
line 2: (x-2) spaces, (star space) twice
line 3: (x-3) spaces, (star space) three times
line 4: (x-4) spaces, (star space) four times
这就是模式。我将把编码留给你。你的问题是空间,因此我建议你考虑一下空间。告诉我:第一颗星左边每行有多少个空间?如果你考虑一下,你可能会解决你自己的问题。试着把它想象成一个网格或矩阵,看看你想在哪里t每行中的“*”以及它与循环索引的关系。很抱歉,我错过了这是家庭作业…将给出一个策略…取而代之
如果你在记事本中做这件事并思考你在做什么,这会很有帮助……你将开始理解你所在的行与空格之间的关系,以及什么不是……在3小时后发布我的答案。我想现在你在@iluxa的建议下几乎完成了它
int height = 20;
for (int level = 1; level <= height; level++)
{
string text = string.Join(" ", Enumerable.Repeat("*", level));
Console.WriteLine(text.PadLeft(height - level + text.Length));
}
int高度=20;
对于(int级别=1;级别<代码>使用系统;
班级计划
{
静态void Main(字符串[]参数)
{
int num,i,j,k;
控制台。写入(“输入级别:”);
num=Convert.ToInt32(Console.ReadLine());
对于(i=1;i使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间金字塔星
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“输入一个数字:”);
int n=Convert.ToInt32(Console.ReadLine());
对于(inti=1;i使用系统;
使用System.Collections.Generic;
使用系统文本;
名称空间星形金字塔
{
班级计划
{
静态void Main(字符串[]参数)
{
程序o=新程序();
o、 show();
Console.ReadKey();
}
无效显示()
{
对于(int i=1;i类程序
{
静态void Main(字符串[]参数)
{
int-num;
控制台写入线(“输入级别”);
num=Int32.Parse(Console.ReadLine());
整数计数=1;
对于(整数行=num;行>=1;行--)
{
对于(int空格=行-1;空格>=1;空格--)
{
控制台。写(“”);
}
对于(int星=1;星< p>),尝试这个,并遵循C、C++、PHP、java < /P>中的相同逻辑
using System;
class pyramid {
static void Main() {
/** Pyramid stars Looking down
Comment this if u need only a upside pyramid **/
int row, i, j;
// Total number of rows
// You can get this as users input
//row = Int32.Parse(Console.ReadLine());
row = 5;
// To print odd number count stars use a temp variable
int temp;
temp = row;
// Number of rows to print
// The number of row here is 'row'
// You can change this as users input
for ( j = 1 ; j <= row ; j++ ) {
// Printing odd numbers of stars to get
// Number of stars that you want to print with respect to the value of "i"?
for ( i = 1 ; i <= 2*temp - 1 ; i++ )
Console.Write("*");
// New line after printing a row
Console.Write("\n");
for ( i = 1 ; i <= j ; i++ )
Console.Write(" ");
// Reduce temp value to reduce stars count
temp--;
}
/** Pyramid stars Looking up
Comment this if u need only a downside pyramid **/
int rowx, k, l;
// Total number of rows
// You can get this as users input
// rowx = Int32.Parse(Console.ReadLine());
rowx = 5;
// To print odd number count stars use a temp variable
int tempx;
tempx = rowx;
//Remove this if u use
Console.Write("\n");
// Number of rows to print
// The number of row here is 'rowx'
for ( l = 1 ; l <= rowx ; l++ ) {
// Leaving spaces with respect to row
for ( k = 1 ; k < tempx ; k++ )
Console.Write(" ");
// Reduce tempx value to reduce space(" ") count
tempx--;
// Printing stars
for ( k = 1 ; k <= 2*l - 1 ; k++ )
Console.Write("*");
// New line after printing a row
Console.Write("\n");
}
}
}
使用系统;
阶级金字塔{
静态void Main(){
/**金字塔星俯视
如果您只需要一个上金字塔,请对此进行注释**/
int行,i,j;
//总行数
//您可以将其作为用户输入
//row=Int32.Parse(Console.ReadLine());
行=5;
//要打印奇数计数星,请使用temp变量
内部温度;
温度=行;
//要打印的行数
//此处的行数为“行”
//您可以将其更改为用户输入
对于(j=1;j,以下代码可能会有所帮助:
public static void print(int no)
{
for (int i = 1; i <= no; i++)
{
for (int j = i; j <= no; j++)
{
Console.Write(" ");
}
for (int k = 1; k < i * 2; k++)
{
if(k % 2 != 0)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.ReadLine();
}
公共静态无效打印(内部编号)
{
对于(int i=1;i这里我创建了一个数字金字塔:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("This is a number pyramid....");
var rows = 5;
for(int i = 1; i <= rows; i++)
{
for(int lsc = (-rows); lsc <= -2; lsc ++)
{
if(lsc < (-1)*i)
{
//write left sided blank spaces
Console.Write(" ");
}
else
{
//write left sided numbers
Console.Write(-1*(lsc));
}
}
for(int rsc = 1; rsc <= rows; rsc++)
{
//write right sided blank spaces
Console.Write(" ");
}
else
{
//Write sided numbers
Console.Write(rsc);
}
}
Console.WriteLine();
}
}
}
使用系统;
公共课程
{
公共静态void Main()
{
Console.WriteLine(“这是一个数字金字塔…”);
var行=5;
对于(inti=1;i我知道这是javascript,但可能会有所帮助
let n = 6;
for (let i = 1; i <= n; i++) {
let spaces = n-i;
let str = '';
for (let j=0; j < spaces; j++) {
str+=' ';
}
for (let j=0; j < i; j++) {
str+='* ';
}
console.log(str)
}
设n=6;
对于(让我=1;我很难在这个问题上提供帮助…大多数帮助就是答案…我想…还有人嫉妒他用C做作业吗?@hunter:怎么会?你希望他用汇编吗?我猜i
循环中的1>=1
是一个打字错误-如果是的话,请编辑。不管其他什么,你都会更容易推理如果只合理地命名变量,请尝试调用循环变量currentLine
和currentstarisk
(或类似)然后将神奇数字5
改为height
或totalines
。看看这是否有帮助。谢谢你发布了答案!虽然代码片段可以回答这个问题,但添加一些附加信息还是很好的,比如解释等等。如果你正在编写这样的程序,它是为了使你的密码更清晰ng逻辑不使用任何内置方法。count变量简化了事情。
using System;
class pyramid {
static void Main() {
/** Pyramid stars Looking down
Comment this if u need only a upside pyramid **/
int row, i, j;
// Total number of rows
// You can get this as users input
//row = Int32.Parse(Console.ReadLine());
row = 5;
// To print odd number count stars use a temp variable
int temp;
temp = row;
// Number of rows to print
// The number of row here is 'row'
// You can change this as users input
for ( j = 1 ; j <= row ; j++ ) {
// Printing odd numbers of stars to get
// Number of stars that you want to print with respect to the value of "i"?
for ( i = 1 ; i <= 2*temp - 1 ; i++ )
Console.Write("*");
// New line after printing a row
Console.Write("\n");
for ( i = 1 ; i <= j ; i++ )
Console.Write(" ");
// Reduce temp value to reduce stars count
temp--;
}
/** Pyramid stars Looking up
Comment this if u need only a downside pyramid **/
int rowx, k, l;
// Total number of rows
// You can get this as users input
// rowx = Int32.Parse(Console.ReadLine());
rowx = 5;
// To print odd number count stars use a temp variable
int tempx;
tempx = rowx;
//Remove this if u use
Console.Write("\n");
// Number of rows to print
// The number of row here is 'rowx'
for ( l = 1 ; l <= rowx ; l++ ) {
// Leaving spaces with respect to row
for ( k = 1 ; k < tempx ; k++ )
Console.Write(" ");
// Reduce tempx value to reduce space(" ") count
tempx--;
// Printing stars
for ( k = 1 ; k <= 2*l - 1 ; k++ )
Console.Write("*");
// New line after printing a row
Console.Write("\n");
}
}
}
public static void print(int no)
{
for (int i = 1; i <= no; i++)
{
for (int j = i; j <= no; j++)
{
Console.Write(" ");
}
for (int k = 1; k < i * 2; k++)
{
if(k % 2 != 0)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.ReadLine();
}
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("This is a number pyramid....");
var rows = 5;
for(int i = 1; i <= rows; i++)
{
for(int lsc = (-rows); lsc <= -2; lsc ++)
{
if(lsc < (-1)*i)
{
//write left sided blank spaces
Console.Write(" ");
}
else
{
//write left sided numbers
Console.Write(-1*(lsc));
}
}
for(int rsc = 1; rsc <= rows; rsc++)
{
//write right sided blank spaces
Console.Write(" ");
}
else
{
//Write sided numbers
Console.Write(rsc);
}
}
Console.WriteLine();
}
}
}
let n = 6;
for (let i = 1; i <= n; i++) {
let spaces = n-i;
let str = '';
for (let j=0; j < spaces; j++) {
str+=' ';
}
for (let j=0; j < i; j++) {
str+='* ';
}
console.log(str)
}