这里不允许使用JAVA变量声明

时间:2015-07-05 13:14:23

标签: java variables if-statement int declaration

我收到错误“此处不允许变量声明”,我不知道为什么,我是java新手,找不到答案:/ 正如它所说,我不能在“if”中制作“int”,但有没有办法创建它?

import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;import java.util.Scanner;
 public class test{
  public static void main(String[] args) throws FileNotFoundException{
    File plik = new File("test.txt");
    PrintWriter saver = new PrintWriter("test.txt");

     int score = 0;
     System.out.println("Q: What's bigger");
     System.out.println("A: Dog B: Ant");
     Scanner odp = new Scanner(System.in);
     string odpo = odp.nextLine();

     if(odpo.equals("a"))
        int score = 1;
     else
         System.out.println("Wrong answer");

  }
}

5 个答案:

答案 0 :(得分:12)

根据Java规范,当没有范围时,您不能声明局部变量。在if中声明int score = 1时,没有范围。 http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html

  

一个局部变量,以下之一   *块中声明的局部变量
  *在for语句中声明的局部变量

此外,您已经在上面声明了一个名为score的变量。即使您删除了该声明,由于上述原因,您也会收到错误。

答案 1 :(得分:2)

string必须更改为String

通过编写int score,您试图声明一个已经存在的新变量,这个变量已经在之前声明过了。只需删除int部分即可获得所需的作业。

答案 2 :(得分:2)

int score = 1;更改为score = 1;

说明:

声明我们使用的变量

someType variable;

要为我们使用的变量分配(或更改)值

variable = value;

我们可以将这些指令混合成一行,如

someType variable = value;

所以当你这样做时

int score = 1;

首先声明变量score,然后为其分配1

这里的问题是我们不能在同一范围内有两个(或更多)同名的局部变量。像

这样的东西
int x = 1;
int x = 2;
System.out.println(x)

不正确,因为我们无法决定在这里使用哪个x

相同

int x = 1;
{
    int x = 2;
    System.out.println(x)
}

因此,如果您只想更改已创建变量的值,请仅使用赋值,不要包含声明部分(删除类型信息)

int x = 1;
//..
x = 2;//change value of x to 2

现在是时候混淆部分范围了。您需要了解变量有一些可以使用的变量。此区域称为范围,并标有{ }括号,其中包含变量声明。所以如果你创建像

这样的变量
{
    int x = 1;
    System.out.println(x); //we can use x here since we are in its scope 
}
System.out.println(x); //we are outside of x scope, so we can't use it here

int x = 2;
System.out.println(x); //but now we have new x variable, so it is OK to use it

因为在

这样的地方有范围限制声明
if (condition)
    int variable = 2;
else
    int variable = 3;

不正确,因为此类代码等于

if (condition){
    int variable = 2;
}else{
    int variable = 3;
}

所以这个变量无法在任何地方访问。

答案 3 :(得分:-1)

不允许在同一范围内声明和初始化相同的名称变量,因此您会发现问题。

您应该只声​​明一次,然后为其重新赋值。

     int score = 0;
     System.out.println("Q: What's bigger");
     System.out.println("A: Dog B: Ant");
     Scanner odp = new Scanner(System.in);
     string odpo = odp.nextLine();

     if(odpo.equals("a"))
        score = 1; // No need to Declare score again here
     else
         System.out.println("Wrong answer");

  }

答案 4 :(得分:-1)

如果您忘记了括号,“此处不允许声明 JAVA 变量”也可能出现。这是因为变量需要声明一个明确的范围。

就我而言,我忘记了在 OUTER 周围使用括号 - 方法内部的 for 循环,这给了我同样的错误。 enter image description here

public static void InsertionSort(int[] list){
        for (int i = 1 ; i < list.length ; i++)

            double currentValue = list[i];
            int k;
            for (k = i - 1 ; k >= 0 && list[k] > currentValue ; k--){

                list[k+1] = list[k];
            }

            // Insert the current element
            list[k+1] = currentValue;

        System.out.println("The sorted array is : "+Arrays.toString(list));
    }