C
C#3y 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
mikernet3y ago
Start by formatting your code please $code
MODiX
MODiX3y 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.avOP3y 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
ero3y ago
what in the world does that mean
p.av
p.avOP3y ago
okay it's meant to be like a triangle so RGGBB BGRB RBG GR B @Ero
ero
ero3y ago
what
p.av
p.avOP3y ago
okay so
ero
ero3y ago
am i stupid or something?
p.av
p.avOP3y ago
there's R G and B
ero
ero3y ago
be more clear please. how do you mean "there is" does the user input that?
p.av
p.avOP3y ago
the same thing adds up next to each other no hold on
ero
ero3y ago
and what do you mean "adds up" R and G don't add up to anything, they're letters
p.av
p.avOP3y ago
p.av
p.avOP3y ago
@Ero i assume something is wrong with my for loop
ero
ero3y ago
that's an interesting task
p.av
p.avOP3y ago
yeahhh it works for an input length of 2 but then falls apart
mikernet
mikernet3y 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.avOP3y ago
right
mikernet
mikernet3y ago
That's basically an if statement, not a for You need i >= 0
p.av
p.avOP3y ago
how would i change that
mikernet
mikernet3y ago
That middle condition is "keep iterating while this is true" "break out when its false"
p.av
p.avOP3y ago
ohhh thank you
Want results from more Discord servers?
Add your server