简体   繁体   中英

checking if number is increasing?

how can i check if number is increasing? 123456-true 423659-false

i try

while (newNumber != 0)
         {

             string a = newNumber.ToString().Substring(0, 1);
             string b = newNumber.ToString().Substring(0, 2);

             input1 = Convert.ToInt32(a);
             input2 = Convert.ToInt32(b);

             if (input1 < input2)
             {
                 ////how do i increase substring a&b +1 ?

             }

         }

    }

also, how can i do it without the substring method?

There's no need to convert the number back and forth between string and int . In C# strings are enumerable as character arrays, and characters are comparable between each other:

bool IsIncreasing(int number)
{
    string text = number.ToString();
    char previous = '0';

    foreach(char c in text)
    {
        if(c <= previous)
            return false;
        previous = c;
    }

    return true;
}

The method you're using isn't a very good one. First of all, b will always be larger than a, because you're feeding 1 and 2 into the length function, not the position. So, for example, if you take the number 12345, a will be 1, and b will be 12. Also, the Substring method is calculated when it's called, so you can't modify the parameters after defining it without calling Substring again. Think of it like this: if you assign the result of a method to a variable, then the method has to have calculated that result, and so if you want a different result, it will have to calculate it again. However, you don't need to use the Substring method at all. In C#, strings can be accessed by index, so you can say myString[0] for the first element (remember, indices in C# are 0 indexed, so the first item is index 0). To do what you're checking, you can use a for loop, like so:

if (newNumber != 0)
     {

         string a = newNumber.ToString();

         for(int i = 1; i < a.Length; i++)
         {
             int input1 = Convert.ToInt32(a[i-1]);
             int input2 = Convert.ToInt32(a[i]);

             if (input1 >= input2) //Note the reversed condition
             {
                 return false; //Gives the false result

             }
         }
         return true; //Computation finished, so the number is increasing
     }

}

This method should be quick, and do pretty much exactly what you want, if I've understood your question correctly. Also, I'm not sure why you were using a while loop, since you don't change "newNumber" in the body of the loop, the program will compute in a loop until something stops it. You should use an if statement. While loops will loop through the body over and over, testing the condition before it starts computation each time, while an if statement only runs once. In general, use if statements where you can, because while loops have the potential to run indefinitely (since you can get into a state where the condition is never met, and it will just loop over and over forever).

您可以看到字符串中索引为i的元素是否大于元素i-1(对于每个索引为1的元素)。

You can use this LINQ chain. asc1 and asc2 are boolean values, the first will be true, the second will be false. What the chain does is to check if the amount of characters with index equals to zero or with index greater than zero but still greater than the previous is equals to the string's length. It doesn't parse the characters because, assuming that the strings are only numbers, their ASCII values will fall in the range 30..39, thus being still valid in the check.

var s1 = "123456";
var s2 = "423659";
var asc1 = s1.Where((c, i) => i == 0 || c > s1[i - 1]).Count() == s1.Length;
var asc2 = s2.Where((c, i) => i == 0 || c > s2[i - 1]).Count() == s2.Length;

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