2012-09-17 7 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

을 당신이 호출 된 메소드에서 사용하는'm' 및'n' 변수를 definining되는 경우'menu'? –

+0

매개 변수 형식이 메서드 구조에 정의되어있는 한 그들의 이름에 관계없이 해당 값을 메서드에 전달할 수 있다고 생각했기 때문에 m 및 n을 사용했습니다. –

답변

1

input varia를 정의하지 않았습니다. menu 메서드 본문에 있습니다. menu 방법에 Scanner input = new Scanner(System.in)을 추가하십시오. main에 변수를 정의하면 이 아니라menu이됩니다. 당신이 Scanner 인스턴스를 여러 번 만들지 않도록하려는 경우, 당신은 그럼 당신은 당신의 모든 방법에서 input를 사용할 수

import java.util.Scanner; 

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

뭔가를 할 수 있습니다.


편집 : 그냥 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명명 된 인수는 N & m 작업을 얻을. 그 중 하나를 변경하십시오. 예를 들면 : 당신은 당신의 프로그램에 input을 정의하지 않았다

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

또한 @ Nambari의 답변은 내 작업을 보완합니다 (+1). – MByD

+0

오류를 6 개로 좁히는 데 도움이 되었습니까? 그 중 일부는 아직 발견되지 않은 기호와 관련이 있습니다. 길잃은 사람은 실제 및 공식 인수 목록의 길이가 다릅니다라고 말합니다. –

1

,하지만 당신은 사용자의 입력을 얻고 싶은 가정

choice=input.nextInt();

를 호출, 당신은

Scanner input = new Scanner(System.in) 

권리가 필요 이전 choice=input.nextInt();

+0

고마워,하지만 메인에 정의 했어? –

+0

main에서 정의한 경우, 매개 변수로 전달해야합니다() (또는) main에서 제거하고 menu()에 넣을 수 있습니다. – kosa

관련 문제