C
C#2y ago
p.av

for loop returning nothing

using System;
using static System.Console;

namespace ColouredTriangles
{
class Program
{
static void Main(string[] args)
{

WriteLine("Enter the first line");
string line = ReadLine();

while (line.Length != 1) // for loop constantly outputting ""
{
string temp = "";
for (int i = line.Length-2; i == 0; i--)
{

if (line[i] == line[i + 1])
{
temp += line[i];
}

else if (line[i].ToString() == "R")
{
if (line[i + 1].ToString() == "G")
{
temp += "B";
}
else
{
temp += "G";
}
}

else if (line[i].ToString() == "G")
{
if (line[i + 1].ToString() == "R")
{
temp += "B";
}
else
{
temp += "R";
}
}

else
{
if (line[i + 1].ToString() == "R")
{
temp += "G";
}
else
{
temp += "B";
}
}
}

WriteLine(temp);
line = temp;

}



}
}
}
using System;
using static System.Console;

namespace ColouredTriangles
{
class Program
{
static void Main(string[] args)
{

WriteLine("Enter the first line");
string line = ReadLine();

while (line.Length != 1) // for loop constantly outputting ""
{
string temp = "";
for (int i = line.Length-2; i == 0; i--)
{

if (line[i] == line[i + 1])
{
temp += line[i];
}

else if (line[i].ToString() == "R")
{
if (line[i + 1].ToString() == "G")
{
temp += "B";
}
else
{
temp += "G";
}
}

else if (line[i].ToString() == "G")
{
if (line[i + 1].ToString() == "R")
{
temp += "B";
}
else
{
temp += "R";
}
}

else
{
if (line[i + 1].ToString() == "R")
{
temp += "G";
}
else
{
temp += "B";
}
}
}

WriteLine(temp);
line = temp;

}



}
}
}
22 Replies
mikernet
mikernet2y ago
Start by formatting your code please $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
p.av
p.av2y ago
alr @mikernet i have done it the code is meant to input a string eg RGBB r and b add to the only one not there so g b and b stay as b
ero
ero2y ago
what in the world does that mean
p.av
p.av2y ago
okay it's meant to be like a triangle so RGGBB BGRB RBG GR B @Ero
ero
ero2y ago
what
p.av
p.av2y ago
okay so
ero
ero2y ago
am i stupid or something?
p.av
p.av2y ago
there's R G and B
ero
ero2y ago
be more clear please. how do you mean "there is" does the user input that?
p.av
p.av2y ago
the same thing adds up next to each other no hold on
ero
ero2y ago
and what do you mean "adds up" R and G don't add up to anything, they're letters
p.av
p.av2y ago
p.av
p.av2y ago
@Ero i assume something is wrong with my for loop
ero
ero2y ago
that's an interesting task
p.av
p.av2y ago
yeahhh it works for an input length of 2 but then falls apart
mikernet
mikernet2y ago
for (int i = line.Length-2; i == 0; i--)
for (int i = line.Length-2; i == 0; i--)
That only runs if i == 0
p.av
p.av2y ago
right
mikernet
mikernet2y ago
That's basically an if statement, not a for You need i >= 0
p.av
p.av2y ago
how would i change that
mikernet
mikernet2y ago
That middle condition is "keep iterating while this is true" "break out when its false"
p.av
p.av2y ago
ohhh thank you