Java 编译错误 "variable not found" 持续存在
Java compilation error "variable not found" persisting
我是 java 编程的新手,当我学习一些理论时,我遇到了一个术语 "Test Cases"。现在我知道当你甚至没有可靠的工作程序时考虑测试用例是没有意义的,但我想从小处着手。
所以我写了一个非常基本的程序来检查号码。对于偶数或奇数。
我在其中添加了一个异常处理块。
这是代码
import java.util.Scanner;
import java.util.InputMismatchException;
class eveodd
{
public static void main (String []args)
{
int num;
System.out.println("Enter the no. ");
try
{
Scanner a = new Scanner(System.in);
num=a.nextInt();
}
catch (InputMismatchException e)
{
System.out.println("Please only enter an integer");
}
finally
{
num=a.nextInt();
}
if (num%2==0)
{
System.out.println(" No. entered is an Even No. ");
}
else
{
System.out.println("No. entered is a Odd no. ");
}
}
}
这是输出:
请告诉我如何解决这个问题。
请修改代码并加粗该部分
谢谢大家:)
a
超出了 finally
子句的范围。您应该在 之前 声明它 try-catch-finally
块,如
Scanner a = new Scanner(System.in);
try {
num=a.nextInt();
} catch (InputMismatchException e) {
System.out.println("Please only enter an integer");
} finally {
num=a.nextInt();
}
我是 java 编程的新手,当我学习一些理论时,我遇到了一个术语 "Test Cases"。现在我知道当你甚至没有可靠的工作程序时考虑测试用例是没有意义的,但我想从小处着手。
所以我写了一个非常基本的程序来检查号码。对于偶数或奇数。 我在其中添加了一个异常处理块。
这是代码
import java.util.Scanner;
import java.util.InputMismatchException;
class eveodd
{
public static void main (String []args)
{
int num;
System.out.println("Enter the no. ");
try
{
Scanner a = new Scanner(System.in);
num=a.nextInt();
}
catch (InputMismatchException e)
{
System.out.println("Please only enter an integer");
}
finally
{
num=a.nextInt();
}
if (num%2==0)
{
System.out.println(" No. entered is an Even No. ");
}
else
{
System.out.println("No. entered is a Odd no. ");
}
}
}
这是输出:
请告诉我如何解决这个问题。 请修改代码并加粗该部分
谢谢大家:)
a
超出了 finally
子句的范围。您应该在 之前 声明它 try-catch-finally
块,如
Scanner a = new Scanner(System.in);
try {
num=a.nextInt();
} catch (InputMismatchException e) {
System.out.println("Please only enter an integer");
} finally {
num=a.nextInt();
}