简体   繁体   中英

why the program is skipping while loop

Why does my program skip the while loop?

class DigitRepeat
{
  public static void main(String args[])
  {
      int n=133,dig=3,digit,count=0;

      while(n!=0)
      {
         digit=n%10;
         if(dig==digit)
         {
            count=count++;
         }
         n=n/10;
      }

      if(count==0)
          System.out.println("digit not found");
      else
          System.out.println("digit occurs "+count+" times.");
  }
}

> count=count++;

should be

> count++;

:

> count=count++;
is
a_temp_var=count;
count=count+1;
count=a_temp_var;
equals to:
a_temp_var=count;
count=a_temp_var;
equals to do nothing.

If I look at the code in my IDE it warns

The value changed at count++ is never used.

ie it is warning me that the value calculated is discarded.

If I step through the code in my debugger, I can see the loop is executed but the line

count = count++;

does not change count .

You want either

count++;

count+=1;

count=count+1;

I do not know what do you mean that the program skips something. But I think I can see your bug. It is here: count=count++;

++ operator increments count after the assumption, so the variable count remains 0 forever.

I think that you wanted to say count++;

You've got a little error in your code:

count = count++;

Should be change to:

count++;

Have a look over here for a running example, all I did was remove the assignment.

(Included for completeness)

  int n = 133;
  int dig = 3;
  int digit;
  int count = 0;

  while (n != 0)
  {
     digit = n % 10;
     if (dig == digit)
     {
        count++;
     }
     n = n / 10;
  }

  if(count = =0)
      System.out.println("digit not found");
  else
      System.out.println("digit occurs " + count + " times."); 

First advice: when you think your program is skipping a loop add a print after the while it will help you narrow down the problem.

Then you have to be careful with post increments. the value countRight is assigned to countLeft then countLeft is incremented but it doesn't matter because the value of count is already set. the value of count is copied therefore when the ++ takes effect its on a different count (sorry if this isn't so clear).

you can use:

count++;

count = count +1;

count += 1;

or count = ++count;

the last one pre increment works because the value is incremented before being copied ^^

(read up on this because it always turns up in interviews ^^

Here is kinda what you're trying to code in C#:

class Program
{
    static void Main(string[] args)
    {
        var number = 12289;
        var search = 2;

        var count = Search(number, search);
    }

    static int Search(int number, int search)
    {
        return number < 10 ?
            (number == search ? 1 : 0) :
            (number % 10 == search ? 1 : 0)
                + Search((int)Math.Floor(number / (double)10), search);
    }
}

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