-1

显示在 If 语句中声明的字符串时出现问题,代码如下:

import java.util.Scanner;

// This program reads a temp and prints its current state
public class P03_01_2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // get temp
        System.out.print("Enter a temperature: ");
        if (in.hasNextDouble()) {
            double temp = in.nextDouble();

            // determine temp type and determine state

            System.out.print("Enter C for Celcius or F for Fahrenheit: ");
            String type = in.nextLine();
            if (type.equals("c") || type.equals("C")) {
                if (temp <= 0) {
                    String state = "frozen";
                } else if (temp >= 100) {
                    String state = "gaseous";
                } else {
                    String state = "water";
                }
            }
            if (type.equals("f") || type.equals("F")) {
                if (temp <= 32) {
                    String state = "frozen";
                } else if (temp >= 212) {
                    String state = "gaseous";
                } else {
                    String state = "water";
                }
            } else if ((!type.equals("c") || !type.equals("C") || !type.equals("f") || !type.equals("F"))) {
                System.out.print("Not valid input.");
                System.exit(0);

            } else {
                System.out.println("Not valid input.");
                System.exit(0);
            }

            System.out.println("The state of the water is " + state);

        }
    }
}

我曾尝试将“String state = null before”放在第一个 if 语句中,但不断出现错误......谢谢。

4

3 回答 3

7

您在每个块中声明变量。即,每个声明都在其自己的块中,并分别限定范围。

String state = null;
if (...) {
   String state = "";
   // this is a different variable to 'state' outside the block!
}

您需要在块之外声明变量,然后在例如

String state = null;
if (...) {
   state = "";
}

// state is still visible here

这是一个关于 Java 中作用域概念的简单教程

于 2013-01-08T11:26:13.213 回答
1

尝试:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    // get temp
    System.out.print("Enter a temperature: ");
    if (in.hasNextDouble()) {
        double temp = in.nextDouble();

        // determine temp type and determine state

        System.out.print("Enter C for Celcius or F for Fahrenheit: ");
        String type = in.nextLine();
        if (type.equals("c") || type.equals("C")) {
            if (temp <= 0) {
                String state = "frozen";
            } else if (temp >= 100) {
                String state = "gaseous";
            } else {
                String state = "water";
            }
        }
        String state = null;
        if (type.equals("f") || type.equals("F")) {
            if (temp <= 32) {
                state = "frozen";
            } else if (temp >= 212) {
                state = "gaseous";
            } else {
                state = "water";
            }
        } else if ((!type.equals("c") || !type.equals("C") || !type.equals("f") || !type.equals("F"))) {
            System.out.print("Not valid input.");
            System.exit(0);

        } else {
            System.out.println("Not valid input.");
            System.exit(0);
        }

        System.out.println("The state of the water is " + state);

    }
}

state必须在范围内...请注意,它现在可能是null. 虽然这不会导致错误...

于 2013-01-08T11:26:51.593 回答
0

如果您尝试输入“String state = null”,那么您应该只在类似 state='frozen' 而不是 String state = freeze 的情况下为“state”分配值。

于 2013-01-08T11:30:06.797 回答