Guys, I do not even know how to formulate a question. Formulated as is, so better look at the simplest code. A week ago, I started reading a tutorial on the basics of Java and solved the problem on it:

import java.io.IOException; public class Registr1 { public static void main(String[] args) throws IOException { int izm=0; for (;;) { char symbol, ignore; do { System.out.print ("Введите букву для изменения ее регистра: "); symbol = (char) System.in.read(); do { ignore = (char) System.in.read(); } while(ignore != '\n'); } while(symbol != '.' & symbol < 'A' | symbol > 'z'); if(symbol >= 'A'&&symbol <= 'Z') System.out.println ("Результат: " + (symbol+=32) + "\n"); else if(symbol >= 'a'&&symbol <= 'z') System.out.println ("Результат: " + (symbol-=32) + "\n"); if(symbol >= 'A' | symbol <= 'z') izm++; if(symbol == '.') break; } System.out.print("Количество измененных символов: " + (izm) + "\n"); } } 

It has a counter that gives out a value 1 more than (I guess) should have. I had to change the last line to:

 System.out.print("Количество измененных символов: " + (--izm) + "\n"); 

Who can explain what I do not understand?

  • Do you consciously use bit operations in logical expressions? - iksuy
  • @iksuy Apparently not. He wrote that he had just begun to learn the basics of the textbook. But this is not the problem, although sooner or later he will face the one described by you. - Max ZS
  • "bit operations in logical expressions" are something for me, as in another language, to read something. Well, for a weekly programming experience, I hope my code will do? - LS15

1 answer 1

Because the condition

 if(symbol >= 'A' | symbol <= 'z') izm++; 

Compiled incorrectly.

If the goal was to consider only characters between A and z , then the condition should look like this:

 if(symbol >= 'A' && symbol <= 'z') izm++; 

In your condition, even the same symbol . falls under it. And, accordingly, before leaving the cycle . is calculated. Why gets under the condition? Because if you write in Russian, it will look something like this:

If the character is greater than or equal to 'A' (number 65) OR the character is less than or equal to 'z' (number 122), then add one.

But the symbol . (number 46) is less than z (number 122).

  • Thank you Everything fell into place - LS15
  • @ LS15 If the answer is correct and helped you, then tick it with a green check mark (under the lower arrow to the left). - Max ZS
  • @ LS15 And also, pay attention to the fact that Iksuy indicated in the commentary under your question - do not use bit operations instead of logical ones. For bit operations, a single operator is used, and for logical operations, a double one. Those. if you still need to write in comparison ИЛИ (precisely in comparison), you would need to write || (twice), and not like you | (once). Note that I wrote in the condition exactly two ampersands && . That's right. A single spelling is an operation on bits in numbers. And the fact that now this is your “roll” does not mean that it will always be like this. - Max ZS
  • Wow ! Well, for the time being, between the & and &&, the only difference is that in the second case the code that will be on the right will not be checked if the code on the left does not match. I hope to figure it out on the sly in that you have just written. - LS15
  • @ LS15 you saw everything correctly ( jls ). Yes, single | and & can be used for bitwise operations on integers, but for Boolean operands they behave as you described. Since there is no difference between & and && , if the right-hand operand is not a function that performs some side effects, then usually use && and || . - zRrr