+ 1

Replace "*" with a sequence of "R, G, B" in C# "Making a Pyramid"

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void DrawPyramid(int n) { for (int i=1; i<=n; i++) { for (int j=i; j<=n; j++) { Console.Write(" "); } for (int k=1; k<=2*i-1; k++) { Console.Write("*"+" "); } Console.WriteLine(); } } static void Main(string[] args) { DrawPyramid(5); } } } This is the code from "Making a Pyramid" in the C# tutorial. I want to hack it so that instead of printing "*" to draw the pyramid, it prints the characters R, G, and B in sequence to the defined DrawPyramid integer. I've had the idea to either make a method that replaces all instances of "*", and tried making a class called RedGreenBlue that would be defined by what the previous char is. This is only my second day trying to learn programming and I really wana figure this out but everything I do blasts my IDE with red circles. Please help!

17th Oct 2017, 11:22 PM
Andrew Straub
Andrew Straub - avatar
5 Respuestas
+ 8
Why it says "Not all code paths return a value" is because it's looking for the "return" keyword. You're not returning, you're assigning to a class variable. Also, the ifs aren't causing a loop. Since SwitchChar() isn't returning a value, it should be: static void SwitchChar()
18th Oct 2017, 6:05 PM
Tamra
Tamra - avatar
+ 9
No need for another class! You could make a class variable that stores the next character to print out. Then, use a method to switch what character it stores. class Program { static char nextChar = 'R'; //Main() //PrintPyramid() //Change the output: Console.Write(nextChar + " "); //Add this line right after it: SwitchChar(); static void SwitchChar() //This method cycles out characters { if (nextChar == 'R') nextChar = 'G'; else if (nextChar == 'G') nextChar = 'B'; else nextChar = 'R'; } }
18th Oct 2017, 12:42 AM
Tamra
Tamra - avatar
+ 9
Whoops, thanks. Fixed it! :D
18th Oct 2017, 4:36 AM
Tamra
Tamra - avatar
+ 4
All right in the previous solution ☺. Just a little typo: nextChar is a class variable so it should be defined static.
18th Oct 2017, 4:35 AM
Ludovico Iommi
Ludovico Iommi - avatar
0
..\Playground\(28,21): error CS0161: 'Program.SwitchChar()': not all code paths return a value using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static char nextChar = 'G'; static void DrawPyramid(int n) { for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { Console.Write(" "); } for (int k = 1; k <= 2 * i - 1; k++) { Console.Write(nextChar + " "); SwitchChar(); } Console.WriteLine(); } } static char SwitchChar() { if (nextChar == 'R') nextChar = 'G'; else if (nextChar == 'G') nextChar = 'B'; else if (nextChar == 'B') nextChar = 'R'; } static void Main(string[] args) { DrawPyramid(15); } } } I think I see what's happening here. SwitchChar() is always cycling through the statement and won't return a valid value because when "else nextChar ='R';", (nextChar == 'R') bools true again. How do we break the loop every time we print nextChar?
18th Oct 2017, 8:40 AM
Andrew Straub
Andrew Straub - avatar