Java 错误:在声明变量之前找不到符号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14595878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:46:37  来源:igfitidea点击:

Java Error: cannot find symbol yet the variables are declared?

javavariablessymbols

提问by Reddy

import java.util.Scanner;
public class Assignment1Q3
{
    public static void main(String[] args) 
    { 
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the first time: ");
    int fTime = in.nextInt();
    System.out.print("Please enter second time: ");
    int lTime = in.nextInt();
    int tDifference = Math.abs(fTime - lTime);
    String strTDiff = String.valueOf(tDifference);
    int length = strTDiff.length();
    if (length == 4)
    {
        String hours = strTDiff.substring(0, 2);
        String minutes = strTDiff.substring(3, 5);
    }
    else if (length == 3)
    {
        String hours = strTDiff.substring(0);
        String minutes = strTDiff.substring(2, 4);
    }
    else
    {
        String hours = ("0");
        String minutes = strTDiff.substring(0, 1);
    }
    System.out.println(hours + " hours " + minutes + " minutes");
    }
}

Hey guys so im attempting to make a simple program using Java to find the difference between 2 times given in military time and to give the output in number of hours and minutes. Whenever i compile this in command prompt it gives me an error for the variable hours and the variable minutes in the last printing line saying "cannot find symbol". I thought trying to declare them before the if statement but that did not work either. I apologize but i am very new to programming and appreciate any help offered.

嘿伙计们,我正在尝试使用 Java 制作一个简单的程序,以找出以军用时间给出的 2 次之间的差异,并以小时数和分钟数给出输出。每当我在命令提示符下编译它时,它都会给我一个关于变量小时和最后打印行中变量分钟的错误,说“找不到符号”。我想尝试在 if 语句之前声明它们,但这也不起作用。我很抱歉,但我对编程很陌生,并感谢提供的任何帮助。

回答by Karthik T

When you define a object reference or any primitive variable inside a ifblock (or any such block), it is accessible only within that block.

当您在if块(或任何此类块)内定义对象引用或任何原始变量时,它只能在该块内访问。

Change as below

更改如下

final String hours, minutes;
if (length == 4)
{
    hours = strTDiff.substring(0, 2);
    minutes = strTDiff.substring(3, 5);
}
else if (length == 3)
{
    hours = strTDiff.substring(0);
    minutes = strTDiff.substring(2, 4);
}
else
{
    hours = ("0");
    minutes = strTDiff.substring(0, 1);
}

回答by Sean F

By calling String within the if statements they only exist within the if statement you need to decalre them outside of the if and associate a value within the if.

通过在 if 语句中调用 String,它们只存在于 if 语句中,您需要在 if 之外对它们进行 decalre 并在 if 中关联一个值。

import java.util.Scanner;
public class Assignment1Q3
{
    public static void main(String[] args) 
    { 
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the first time: ");
    int fTime = in.nextInt();
    System.out.print("Please enter second time: ");
    int lTime = in.nextInt();
    int tDifference = Math.abs(fTime - lTime);
    String strTDiff = String.valueOf(tDifference);
    int length = strTDiff.length();
    String hours = "";
    String minutes = "";

    if (length == 4)
    {
        hours = strTDiff.substring(0, 2);
        minutes = strTDiff.substring(3, 5);
    }
    else if (length == 3)
    {
        hours = strTDiff.substring(0);
        minutes = strTDiff.substring(2, 4);
    }
    else
    {
        hours = ("0");
        minutes = strTDiff.substring(0, 1);
    }
    System.out.println(hours + " hours " + minutes + " minutes");
    }
}

so you can initialise them to "" or to remove unnecessary else statement initialise them to the else valuse

所以你可以将它们初始化为 "" 或删除不必要的 else 语句将它们初始化为 else 值

    String hours = "0";
    String minutes = strTDiff.substring(0, 1);

    if (length == 4)
    {
        hours = strTDiff.substring(0, 2);
        minutes = strTDiff.substring(3, 5);
    }
    else if (length == 3)
    {
        hours = strTDiff.substring(0);
        minutes = strTDiff.substring(2, 4);
    }
System.out.println(hours + " hours " + minutes + " minutes");