找不到符号:Java [英] Cannot find Symbol: Java
问题描述
我很抱歉,如果这是一个奇怪的问题,但我刚刚开始OOP并遇到了一个简单的菜单驱动的数学程序我应该做的问题。我清除了编译器给我的所有错误,但现在它给了我大约14个新错误,其中大多数被描述为'找不到符号'。这是我的代码:
I'm sorry if this is a weird question but I've just started OOP and ran across this problem for a simple menu driven math program that I was supposed to make. I cleared all the errors the compiler gave me but now it's given me about 14 fresh errors most of which are described as 'cannot find symbol.' Here's my code:
import java.util.Scanner;
public class MathMenu
{
//MENU METHOD
private static void menu(String args[])
{
int choice;
System.out.printf("Enter '1' to add");
System.out.printf("Enter '2' to subtract");
System.out.printf("Enter '3' to exit");
System.out.printf("\nPlease enter your choice: ");
choice=input.nextInt();
if (choice==1)
sum(n,m);
if (choice==2)
dif(n,m);
else if(choice==3)
return;
}
//SUM
private static int sum(int a, int b)
{
return n+m;
}
//DIFFERENCE
private static int dif(int a, int b)
{
if(n<m)
return m-n;
else
return n-m;
}
public static void main(String args[])
{
int n=15;
int m=8;
Scanner input = new Scanner(System.in);
menu();
}
}
这是新的编译器输出:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Shahraiz Tabassam>cd c:\java\bin
c:\java\bin>javac MathMenu.java
MathMenu.java:7: error: no suitable constructor found for Scanner()
private static Scanner input = new Scanner();
^
constructor Scanner.Scanner(ReadableByteChannel,String) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(ReadableByteChannel) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(String) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(Path,Charset) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(Path,String) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(Path) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(File,CharsetDecoder) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(File,String) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(File) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(InputStream,String) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(InputStream) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(Readable) is not applicable
(actual and formal argument lists differ in length)
constructor Scanner.Scanner(Readable,Pattern) is not applicable
(actual and formal argument lists differ in length)
MathMenu.java:64: error: method menu in class MathMenu cannot be applied to give
n types;
menu();
^
required: String[]
found: no arguments
reason: actual and formal argument lists differ in length
2 errors
c:\java\bin>
推荐答案
您从未定义输入
变量。尝试在菜单
方法体内的菜单
方法中添加扫描仪输入=新扫描仪(System.in)
。只需在 main
中定义变量就可以不给菜单
访问它。如果你想避免多次创建一个 Scanner
实例,你可以做类似的事情
You never defined your input
variable in the body of the menu
method. Try adding Scanner input = new Scanner(System.in)
within the menu
method. Simply defining the variable in main
does not give menu
access to it. If you want to avoid creating a Scanner
instance multiple times, you could do something like
import java.util.Scanner;
public class MathMenu {
private static Scanner input = new Scanner(System.in);
...
}
然后你可以使用从您的所有方法输入
。
编辑:我刚注意到类似于
m
和 n
:你必须在它们被使用的方法中定义它们,或者使它们成为 static
字段。如果由我决定,我会这样做:
Then you could use input
from all of your methods.
EDIT: I just noticed something similar for
m
and n
: you have to define them within the method in which they are being used, or make them static
fields. If it was up to me I'd do it like this:
import java.util.Scanner;
public class MathMenu {
private static Scanner input = new Scanner(System.in);
private static int n = 15;
private static int m = 8;
// ...
// your other methods unchanged
// ...
public static void main(String[] args) {
menu(args); // or just "menu()" if you remove the arguments from the menu method declaration.
}
}
这篇关于找不到符号:Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!