开发者

Problem with loop in Java

开发者 https://www.devze.com 2023-01-03 06:42 出处:网络
What is the mistake in the following code? while ((char t==(char) System.in.read开发者_JS百科())!=\'0\')

What is the mistake in the following code?

 while ((char t==(char) System.in.read开发者_JS百科())!='0')


You can not declare a new variable in a while loop.

    while (boolean always = true) {
    } // DOES NOT COMPILE!!!

You'd have to declare the variable before and outside of the loop, so perhaps something like this:

    boolean always = true;
    while (always) {
        break;
    } // compiles fine!

    // always is still in scope after the loop!
    always = !always;

In this sense, for loop is unique: you can in fact declare a new local variable whose scope is limited to that loop:

    for (boolean always = true; always; ) {
        break;
    } // compiles fine!

    // always is no longer declared after the loop!
    always = !always; // DOES NOT COMPILE!

That said, looking at what you're doing, you may want to look at java.util.Scanner. I suspect that it will serve your need much better.


Example

Here's an example of using Scanner to read numbers from standard input, terminating at 0. It then prints the sum of those numbers. It handles invalid input gracefully using hasNextInt() instead of Integer.parseInt/NumberFormatException.

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter numbers (0 to end):");
    int sum = 0;
    int number;
    do {
        while (!sc.hasNextInt()) {
            System.out.println("I'm sorry, that's not a number! Try again!");
            sc.next();
        }
        number = sc.nextInt();
        sum += number;
    } while (number != 0);
    System.out.println("The sum of those numbers is " + sum);

Here's an example session:

Enter numbers (0 to end):
1
3
-1
five
I'm sorry, that's not a number! Try again!
2
0
The sum of those numbers is 5


This is probably what you intended to write.

char t;
while ((t = (char) System.in.read()) != '0') {
    //...
}


 while ((char t==(char) System.in.read())!='0')
 //            ^^  should be 'char t = ...'

This loop can be rewritten more clearly as

 while (true) {
    char t = (char) System.in.read();
    if (t == '0')
      break;
    ...


char t is a statement which declares a variable rather than an expression; it has no value to compare with the == operator. You probably also meant to use assignment rather than equality, but unlike C++ the declaration does not have a value.

The easiest way of restricting the scope of a variable in a loop is to use for instead, which specifically allows you to declare variables.

for (char t; ( t = (char) System.in.read() ) != '0'; )
   // loop body involving t

However, some company's guidelines don't allow mutating operators in boolean expressions, such as the read and the assignment, and would prefer you to separate them out onto several lines. Personally I find restricting the scope more important.

0

精彩评论

暂无评论...
验证码 换一张
取 消