简体   繁体   中英

What's wrong with my C# For Loop and If statement?

int LetterCount = 0;
string strText = "Debugging";
string letter;

for (int i = 0; i <strText.Length; i++)
{
  letter = strText.Substring(0, 9);
  if(letter == "g")
  {    
    LetterCount++;
    textBox1.Text = "g appears " + LetterCount + " times";
  }
}

So, I'm doing this tutorial thing, and I've been stuck on this exercise for like 4 hours. And I can't figure out what's wrong with my For Loop.

The point of the exercise is to make my program thing tell me how many g's are in the word debugging. But you probably figured that out. Anyway, I'm not even sure that I have the right code for telling me that, because I think that I need to change the second part of the For Loop (the i < ) part.

But my problem is that it isn't registering the "if letter == "g" " at all. Because according to my locals window it says that letter=Debugging, which would make me think that g should be registering on my program 24 times, I think (because str.length is 9 letters long?) But it's registering as 0 no matter what I do.

You are extracting a string of 9 characters. It will never be equal to "g" (which only has one). Here's how I'd do it.

int count = 0;
foreach (char c in strText)
{
    if (c == 'g')
       count++;
}

Using the for loop:

for (int i = 0; i < strText.Length; i++)
{
    if (strText[i] == 'g')
       count++;
}

Take a look at the documentation for string.Substring(x, y) .

Basically:

letter = strText.Substring(0, 9);

Isn't giving you a letter. Each time through it's giving you all 9 characters of the string strText . You might want to consider using the variable i for one of the values you pass to Substring.

(I've deliberately not given you the entire answer as you seem to want to understand, so, if the pointers I've given don't get you there, let me know and I'll expand my answer =)

Try this:

    for (int i = 0; i <strText.Length; i++)
    {

       if(strText[i] == 'g')
       {
         LetterCount++;
       }
    }
    textBox1.Text = "g appears " + LetterCount + " times";

The issue is that you are looking at the entire string when you compare to "g". By specifying an index you are telling it to look at a specific character in the string. Also, I removed your substring because it did not appear to be doing anything.

You're not using i at all in your for loop.

Do you mean

letter = strText.Substring(i, 1);

?

Well, you are taking substring that is long 9 charachters and comparing it to "g". It won't be equal.

You should try:

letter = strText.Substring(i,1);

Because String.Substring(int, int) takes two arguments: the offset and amount to take.

In your case, letter = strText.Substring(0, 9); will simply assign letter's value to "Debugging". If you want to check each letter individually, you need to write letter = strText.Substring(i, 1) .

You're probably looking for something like this:

int LetterCount = 0;
string strText = "Debugging";
string letter;

for (int i = 0; i <strText.Length; i++)
{
  letter = strText.Substring(i, 1);
  if(letter == "g")
  {    
    LetterCount++;
    textBox1.Text = "g appears " + LetterCount + " times";

  }
}

letter = strText.Substring(0, 9);

at this point, 'letter' has the value "Debugging" since you're taking the entire string.

Try letter = strText[i] so you isolate the single letter.

What @Rob said.

Try something like this:

int    gCount = 0;
string s      = "Debugging";

for ( int i = 0; i <strText.Length; i++)
{
  if ( s[i] == 'g' ) ++gCount ;
}
textBox1.Text = "g appears " + gCount+ " times";
namespace runtime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            int lettercount = 0;
            string strText = "Debugging";
            string letter;


            for (int i = 0; i < strText.Length; i++)
            {
                letter = strText.Substring(i,1);

                if (letter == "g")
                {
                    lettercount++;
                }

            }
            textBox1.Text = "g appear " + lettercount + " times";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM