+ 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
7 odpowiedzi
+ 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
0
Bitcoin and other cryptocurrencies have turned into a pretty valuable digital asset for a lot of people globally. But the fear of losing access to these digital funds is a big worry. That's where companies like METATECH RECOVERY PRO come in, using their super-smart analysis tools to give folks a fighting chance if they've lost their Bitcoins. METATECH RECOVERY PRO is a top-notch service that specializes in getting back lost cryptocurrency, especially when it comes to finding and recovering lost Bitcoins. Their super-detailed analysis tools are key to their success, giving them the precision and know-how needed in the complicated world of digital money. METATECH RECOVERY PRO offers a bunch of services to help people get back their lost or inaccessible cryptocurrencies. Their team of experts uses advanced analysis tools to give quick and effective solutions that are customized for each person. When it comes to getting back cryptocurrency, analysis tools are super important for tracking transactions, spotting patterns, and getting back lost funds.These techniques help them get the best results for their clients and speed up the recovery process. METATECH RECOVERY PRO works with experienced blockchain experts who know their stuff about cryptocurrency protocols and technologies. By combining their expertise with advanced analysis tools, METATECH RECOVERY PRO can offer complete solutions that handle even the toughest cases of lost Bitcoin recovery. There was a time when getting back Bitcoin was like a dream, but thanks to dedicated teams like METATECH RECOVERY PRO, that's changing. With a focus on being more precise and efficient, the days of lost Bitcoins being gone forever might be over soon. Connect with META TECH RECOVERY PRO on all platforms or for help via: W/A ‪‪‪+1 469-692‑8049‬‬‬ ‪https://metatech-recoverypro.com‬ Metatech@Writeme.Com Thank you.
21st Jul 2025, 8:14 AM
winston carol
0
HIRE THE BEST BITCOIN RECOVERY EXPERT; TRACK AND RECOVER LOST CRYPTOCURRENCY WITH META TECH RECOVERY PRO Bitcoin and other cryptocurrencies have turned into a pretty valuable digital asset for a lot of people globally. But the fear of losing access to these digital funds is a big worry. That's where companies like METATECH RECOVERY PRO come in, using their super-smart analysis tools to give folks a fighting chance if they've lost their Bitcoins. METATECH RECOVERY PRO is a top-notch service that specializes in getting back lost cryptocurrency, especially when it comes to finding and recovering lost Bitcoins. Their super-detailed analysis tools are key to their success, giving them the precision and know-how needed in the complicated world of digital money. METATECH RECOVERY PRO offers a bunch of services to help people get back their lost or inaccessible cryptocurrencies. Their team of experts uses advanced analysis tools to give quick and effective solutions that are customized for each person. When it comes to getting back cryptocurrency, analysis tools are super important for tracking transactions, spotting patterns, and getting back lost funds.These techniques help them get the best results for their clients and speed up the recovery process. METATECH RECOVERY PRO works with experienced blockchain experts who know their stuff about cryptocurrency protocols and technologies. By combining their expertise with advanced analysis tools, METATECH RECOVERY PRO can offer complete solutions that handle even the toughest cases of lost Bitcoin recovery. There was a time when getting back Bitcoin was like a dream, but thanks to dedicated teams like METATECH RECOVERY PRO, that's changing. With a focus on being more precise and efficient, the days of lost Bitcoins being gone forever might be over soon. Connect with META TECH RECOVERY PRO on all platforms or for help via: W/A ‪‪‪+1 469-692‑8049‬‬‬ ‪https://metatech-recoverypro.com‬ Metatech@Writeme.Com Thank you.
21st Jul 2025, 9:06 AM
winston carol