【发布时间】:2017-09-24 20:59:44
【问题描述】:
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
do {
System.out.println("What is the command keyword to exit a loop in Java?\na. int\nb. continue\nc. break\nd. exit\nEnter your choice");
Scanner ans = new Scanner(System.in);
char ans = sc.next().charAt(0);
if(ans=='c')
System.out.println("Correct");
else
System.out.println("Wrong! Presss Y to try again.");
Scanner redo = new Scanner(System.in);
char redo = sc.next().charAt(0);
}
while(redo=='y');
}
}
我刚开始学习 Java,你能告诉我我的代码有什么问题以及如何改进它吗?谢谢
这是我收到的错误。
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Duplicate local variable ans
sc cannot be resolved
Duplicate local variable redo
sc cannot be resolved
redo cannot be resolved to a variable
at Application.main(Application.java:9)
【问题讨论】:
-
你为什么认为这是错误的?
-
它不起作用。这是错误...线程“main”java.lang.Error中的异常:未解决的编译问题:无法解决重复的局部变量ans sc无法解决重复的局部变量redo sc无法解决重做无法解析为Application.main的变量(Application.java:9)
-
请不要在 cmets 中发布错误。请改为edit您的问题。
-
我已经编辑了我的问题
-
好。现在您觉得这些错误消息的哪一部分不清楚?