Challenge: Methods Creation
Following is a code for outputting a triangle shape using multiple Console.WriteLine
statements:
main.cs
9
1
2
3
4
5
Console.WriteLine("*********");
Console.WriteLine("*******");
Console.WriteLine("*****");
Console.WriteLine("***");
Console.WriteLine("*");
12345Console.WriteLine("*********"); Console.WriteLine("*******"); Console.WriteLine("*****"); Console.WriteLine("***"); Console.WriteLine("*");
Create a new method called displayTriangle
with the above code and call it in the main function.
main.cs
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
namespace ConsoleApp
{
internal class Program
{
// Create a method here
static void Main(string[] args)
{
// Call the function here
}
}
}
123456789101112131415using System; namespace ConsoleApp { internal class Program { // Create a method here static void Main(string[] args) { // Call the function here } } }
Create a method using static void methodName()
syntax and put the triangle code inside it.
Afterwards, execute it in the main method.
main.cs
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
namespace ConsoleApp
{
internal class Program
{
// Create a method here
static void displayTriangle()
{
Console.WriteLine("*********");
Console.WriteLine("*******");
Console.WriteLine("*****");
Console.WriteLine("***");
Console.WriteLine("*");
}
static void Main(string[] args)
{
// Call the method here
displayTriangle();
}
}
}
1234567891011121314151617181920212223using System; namespace ConsoleApp { internal class Program { // Create a method here static void displayTriangle() { Console.WriteLine("*********"); Console.WriteLine("*******"); Console.WriteLine("*****"); Console.WriteLine("***"); Console.WriteLine("*"); } static void Main(string[] args) { // Call the method here displayTriangle(); } } }
Var alt klart?
Tak for dine kommentarer!
Sektion 6. Kapitel 3
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat