2012-09-17 143 views
-1

對不起,如果這是一個奇怪的問題,但我剛剛開始OOP,並遇到了一個簡單的菜單驅動的數學程序,我應該做的這個問題。我清除了編譯器給我的所有錯誤,但現在它給了我大約14個新錯誤,其中大部分被描述爲'找不到符號'。這裏是我的代碼:找不到符號:Java

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> 
+0

你在哪裏定義你在'menu'方法中使用的'm'和'n'變量? –

+0

我用m和n是因爲我認爲我可以將它們的值傳遞給方法,而不管它們的名稱是什麼,只要參數類型是在方法結構中定義的。 –

回答

1

您從未定義過您的input變異在menu方法的身體。嘗試在menu方法內添加Scanner input = new Scanner(System.in)。簡單地定義main中的變量不是menu訪問它。如果你想避免創建Scanner實例多次,你可以不喜歡

import java.util.Scanner; 

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

那麼你可以使用所有的你的方法input


編輯:我只是注意到了 mn類似的東西:你必須在正在使用他們的方法中定義它們,或讓他們 static領域。如果這取決於我,我會這樣做:

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. 
    } 
} 
+0

馬上試試。謝謝! –

+0

我覺得你正在做點什麼。你能舉出一個代碼示例嗎? –

+0

看到上面的編輯 – arshajii

1

您的所有功能得到命名一個& b,但論點ň&米工作。改變其中之一。例如:

private static int sum(int n, int m) 
{ 
    return n+m; 
} 
+0

另外,@ Nambari的答案是對我的補充(+1)。 – MByD

+0

是否做到了這一點,它有助於將錯誤縮小到6個。其中一些仍然與未找到的符號有關,另一個則說實際和正式參數列表的長度不同。 –

1

你沒有在你的程序中定義input,但調用

choice=input.nextInt();

假設你想從用戶那裏得到輸入,你需要有

Scanner input = new Scanner(System.in) 

權之前choice=input.nextInt();

+0

謝謝,但我在主要定義? –

+0

如果你在main中定義了它,你需要將它作爲參數傳遞給menu()(或者),你可以將它從main中移除並放入菜單()中。 – kosa