关于oop:找不到符号:Java

Cannot find Symbol: Java

本问题已经有最佳答案,请猛点这里访问。

我很抱歉如果这是一个奇怪的问题,但我刚刚开始面向对象编程,并通过一个简单的菜单驱动的数学程序,我应该做这个问题。我清除了编译器给我的所有错误,但现在它给了我大约14个新的错误,其中大部分被描述为"找不到符号"。下面是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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("
Please 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();

}


}

下面是新的编译器输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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>


您从未在menu方法的主体中定义您的input变量。尝试在menu方法中添加Scanner input = new Scanner(System.in)。仅仅在main中定义变量并不能使menu访问它。如果您希望避免多次创建一个Scanner实例,那么您可以执行如下操作

1
2
3
4
5
6
import java.util.Scanner;

public class MathMenu {
    private static Scanner input = new Scanner(System.in);
    ...
}

然后您可以使用所有方法中的input


编辑:我刚注意到mn有类似的地方:你必须在使用它们的方法中定义它们,或者使它们成为static字段。如果由我决定,我会这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.
    }
}


您没有在程序中定义EDCOX1×0,但调用

choice=input.nextInt();

假设你想从用户那里得到输入,你需要

1
Scanner input = new Scanner(System.in)

就在choice=input.nextInt();之前


所有函数都会得到名为a&b的参数,但可以使用n&m。请更改其中一个参数。例如:

1
2
3
4
private static int sum(int n, int m)
{
    return n+m;
}