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

Java:访问if语句中的字符串,java,string,if-statement,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

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

        }
    }
}
import java.util.Scanner;
//此程序读取临时文件并打印其当前状态
公共类P03\U 01\U 2{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
//临时工
系统输出打印(“输入温度:”);
if(in.hasNextDouble()){
双温=in.nextDouble();
//确定温度类型并确定状态
System.out.print(“输入C表示Celcius,输入F表示华氏:”;
字符串类型=in.nextLine();
if(type.equals(“c”)| type.equals(“c”)){
如果(温度=100){
字符串状态=“气态”;
}否则{
字符串状态=“水”;
}
}
if(type.equals(“f”)| type.equals(“f”)){
如果(温度=212){
字符串状态=“气态”;
}否则{
字符串状态=“水”;
}
}如果(!type.equals(“c”)| |!type.equals(“c”)| |!type.equals(“f”)| |!type.equals(“f”)){
系统输出打印(“无效输入”);
系统出口(0);
}否则{
System.out.println(“无效输入”);
系统出口(0);
}
System.out.println(“水的状态为”+状态);
}
}
}

我试着在第一个if语句之前加上“String state=null”,但总是出错……谢谢。

您在每个块中声明变量。i、 e.每个声明都在其自己的块中,并单独确定范围

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中作用域概念的例子。

试试:

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);

    }
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
//临时工
系统输出打印(“输入温度:”);
if(in.hasNextDouble()){
双温=in.nextDouble();
//确定温度类型并确定状态
System.out.print(“输入C表示Celcius,输入F表示华氏:”;
字符串类型=in.nextLine();
if(type.equals(“c”)| type.equals(“c”)){
如果(温度=100){
字符串状态=“气态”;
}否则{
字符串状态=“水”;
}
}
字符串状态=null;
if(type.equals(“f”)| type.equals(“f”)){
如果(温度=212){
state=“气态”;
}否则{
state=“水”;
}
}如果(!type.equals(“c”)| |!type.equals(“c”)| |!type.equals(“f”)| |!type.equals(“f”)){
系统输出打印(“无效输入”);
系统出口(0);
}否则{
System.out.println(“无效输入”);
系统出口(0);
}
System.out.println(“水的状态为”+状态);
}
}

状态
必须在范围内。。。请注意,它现在可能是
null
。但是这不会导致错误…

如果您试图输入'String state=null',那么您应该只在类似state='freezed'而不是String state=freezed的情况下在其中为'state'赋值。

谢谢,我上周一直在学习java,这已经持续了几个小时了。