此处不允许 JAVA 变量声明 [英] JAVA Variable declaration not allowed here
本文介绍了此处不允许 JAVA 变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误此处不允许使用变量声明",我不知道为什么,我是 Java 新手,找不到答案:/正如它所说,我不能在if"中创建int",但有没有办法创建它?
I get an error "Variable declaration not allowed here" and I don't know why, I'm new in java and can't find answer :/ As it says, I can't make "int" in "if" but is there a way to create it?
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");
}
}
推荐答案
string
必须改成String
.
通过编写 int score
,你试图声明一个已经存在的新变量,你之前已经声明过.只需删除 int
部分,你就会得到你想要的分配.
By writing int score
you're trying to declare a new variable that already exists, which you declared before already. Just remove the int
part and you will get the assignment you want.
这篇关于此处不允许 JAVA 变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文