【发布时间】:2013-10-25 10:00:54
【问题描述】:
我正在编写 Java 代码:
import java.util.Scanner;
public class main
{
static Scanner console = new Scanner (System.in);
public static void main (String aurgs[]) {
System.out.print("Which function would you like to vist? L for Lottery, R for Random Math");
char lotto = 'l';
char rdm = 'b';
if (console.nextChar() = lotto) /**error is here*/ {
lottery();
}
}
public static void lottery (String aurgs[]) {
String announ = "Your lottery numbers for today are: ";
System.out.print(announ);
int [] numbers = {15, 68, 25, 66};
double lucky = 50.2;
for (int i: numbers )
System.out.print(i + "\n");
String announ2 = "Your lucky number is: ";
System.out.println(announ2 + lucky);
}
}
当我编译它时,BlueJ 说:
找不到符号 - 方法 nextChar()
我不明白出了什么问题。
【问题讨论】:
-
您不能只是编造方法并期望它们起作用。不,编程是一种精确的练习,编译器是无情的。您需要学习使用 Java API 并大量使用它。
-
这可能是因为您在 if 子句中使用了赋值运算符 = 而不是比较运算符 ==,但 @Hovercraft 可能是正确的。
-
更具体一点:没有
Scanner#nextChar()方法。 (但是有很多类似的问题,这让我觉得有些教授或课堂笔记以错误的断言开头..) -
使用一些IDE,比如Eclipse,他们会告诉你有哪些方法可供你使用。