我正在尝试自己学习编程,我正在从一本书中找到一个我无法解决的问题:
允许用户输入两个值:用于打印等腰三角形的字符和三角形的峰值大小。例如,如果用户为字符输入#,为峰值输入6,则应生成以下显示:
#
##
###
####
#####
######
#####
####
###
##
#
这是我到目前为止的代码:
char character;
int peak;
InputValues(out character, out peak);
for (int row = 1; row < peak * 2; row++)
{
for (int col = 1; col <= row; col++)
{
Console.Write(character);
}
Console.WriteLine();
}
Console.Read() // hold console open
提前致谢。
答案 0 :(得分:2)
关闭,但是当你'退缩'时你想开始减少。
你可以做两个循环;左0 -> peak
,然后是(peak - 1 -> 0)
,它会打印两个'方向'。
另一种方法是找出你在行数方面距离峰值有多远,并打印出那么多字符。
for (int row = 0; row < peak*2; row++)
{
for(var i = 0; i < peak - Math.Abs(row - peak); i++)
Console.Write(character);
Console.WriteLine();
}
答案 1 :(得分:2)
for (int row = 0; row < peak; row++)
{
Console.WriteLine(new string(character, row + 1));
}
for (int row = 1; row < peak; row++)
{
Console.WriteLine(new string(character, peak - row));
}
答案 2 :(得分:1)
Yuriy Faktorovich回答的轻微替代品(我从来没有使用降压因此我无法抗拒)
警告未经测试
for (int row = 0; row < peak; row++)
{
Console.WriteLine(new string(character, row + 1));
}
for (int row = peak, row > 1; row--)
{
Console.WriteLine(new string(character, row));
}
答案 3 :(得分:1)
我喜欢的方式:
static void drawIsoscelesTraiangle(char repeatChar, int peak, int current)
{
for (int i = 0; i < peak; i++)
{
Console.WriteLine(new String(repeatChar, current++));
}
for (int i = current; i > 0; i--)
{
Console.WriteLine(new String(repeatChar, current--));
}
}
答案 4 :(得分:0)
这是一个带循环的版本和一个带LINQ ...
的版本Console.WriteLine("peak");
var peak = int.Parse(Console.ReadLine());
Console.WriteLine("char");
var @char = (char)Console.Read();
//As LINQ
var query = (from row in Enumerable.Range(0, peak * 2)
let count = peak - Math.Abs(peak - row)
let str = new string(@char, count)
select str
).ToArray(); //if you have .Net 4.0+ you don't need the .ToArray()
Console.WriteLine(string.Join(Environment.NewLine, query));
//As Loop
for (int r = 1; r < peak * 2; r++)
Console.WriteLine("{0}", new string(@char, peak - Math.Abs(peak - r)));
Console.Read(); // hold console open
答案 5 :(得分:0)
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char drawChar = '#';
int repeatNumber = 5;
Program.drawIsoscelesTraiangle(drawChar, repeatNumber, 1);
Console.ReadKey();
}
static void drawIsoscelesTraiangle(char repeatChar, int peak, int current)
{
if (current < peak)
{
Console.WriteLine(new string(repeatChar, current));
Program.drawIsoscelesTraiangle(repeatChar, peak, current + 1);
Console.WriteLine(new string(repeatChar, current));
}
else
{
Console.WriteLine(new string(repeatChar, current));
}
}
}
}
我没有注意获取用户输入,而是硬编码。但如果我不理解错误,这就是你想要的。如果你想一想就很简单:)这会使用一种叫做“递归”的策略,如果你还没有,你应该学习它。
希望这会有所帮助。此致
答案 6 :(得分:0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch6ex10
{
class Isosceles
{
static void Main(string[] args)
{
OutputTraiangle(InputCharacter(), InputPeak());
Console.ReadLine();
}
//Allow user to input a character to be used as the triangle
public static char InputCharacter()
{
string inputValue;
char character;
Console.Write("Enter the character to be used to display the triangle: ");
inputValue = Console.ReadLine();
character = Convert.ToChar(inputValue);
return character;
}
//Allow user to input an integer for the peak of the triangle
public static int InputPeak()
{
string inputPeak;
int peak;
Console.Write("Enter the integer amount for the peak of the triangle: ");
inputPeak = Console.ReadLine();
peak = Convert.ToInt32(inputPeak);
return peak;
}
//Output the triangle using the users character choice and peak size (first half)
public static void OutputTraiangle(char character, int peak)
{
int start = 1;
int finish = (peak - 1);
while (start != peak)
{
for (int amount = 1; amount <= start; amount++)
{
Console.Write(character);
}
Console.WriteLine();
start++;
}
for (int amount = 1; amount <= peak; amount++)
{
Console.Write(character);
}
Console.WriteLine();
while (finish != 0)
{
for (int amount = 1; amount <= finish; amount++)
{
Console.Write(character);
}
Console.WriteLine();
finish--;
}
}
}
}