Java:在if语句中访问字符串

时间:2013-01-08 11:24:04

标签: java string if-statement

显示在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);

        }
    }
}

我已经尝试在第一个if语句之前添加“String state = null”,但是一直有错误.......谢谢。

3 个答案:

答案 0 :(得分: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范围概念的simple tutorial

答案 1 :(得分: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。这不会导致错误......

答案 2 :(得分:0)

如果你试图把'String state = null'那么你应该只在内部指定'state'的值,如果state ='frozen'而不是String state = frozen。