2017-03-27 1 views
-3

두 개의 서로 다른 데이터 유형으로 메소드 오버로드를 구현했습니다. 이것이 내가 코드로 끝난 방법입니다. 하지만 이제 기호 c와 d를 찾을 수 없습니다. 어떤 도움?함수 호출에서 '심볼을 찾을 수 없습니다'를 수정하는 방법은 무엇입니까?

import java.util.*; 
import java.lang.*; 
public class LargestOfTwoTest{ 
    public static void main(String args[]) throws Exception{ 
     Scanner scan = new Scanner(System.in); 



     System.out.println("Enter two numbers, and I wiil show you which one's largest!\n"); 
     System.out.println("Enter two numbers: "); 
     double a = scan.nextDouble(); 
     double b = scan.nextDouble(); 

     if (a==(Math.floor(a))){ 
      int c = (int) a; 
     } 
     else{ 
      double c = a; 
     } 

     if (b==(Math.floor(b))){ 
      int d = (int) b; 
     } 
     else { 
      double d = b; 
     } 

     System.out.print("Largest of the numbers is "+largest(c,d)); 
    } 




    public static int largest(int x, int y){ 
     if (x>y) 
      return x; 
      //System.out.print("Largest of the numbers is "+x); 
     else 
      return y; 
      //System.out.print("Largest of the numbers is "+y); 
    } 
    public static double largest(double x, double y){ 
     if (x>y) 
      return x; 
      //System.out.print("Largest of the numbers is "+x); 
     else 
      return y; 
      //System.out.print("Largest of the numbers is "+y); 
    } 
} 

은 ..이 광고

System.out.print("Largest of the numbers is "+largest(c,d)); 

에서 오류가 표시

LargestOfTwoTest.java:29 오류 : 기호를

(c 및 d)

+5

'c'와 'd'를 귀하의 인보이스 범위에서 제외했습니다 '최대'의 양이온은 오버로드와 아무런 관련이 없습니다. – Mena

+0

단일 범위에서 하나의 변수 선언 만 가질 수 있습니다. 선언을'if'에 넣음으로써 다른 선언을 생성하려는 시도는'if' 블록이 분리 된 범위이고 블록이 완료 되 자마자 선언이 범위를 벗어나기 때문에 작동하지 않습니다. – RealSkeptic

+0

제목을 특정 질문으로 바꾸거나 계속 다운 voted 상태로 유지해야합니다. 그냥 친절한 머리. – samosaris

답변

0
import java.math.BigDecimal; 
import java.util.Scanner; 

public class LargestOfTwo { 

    private Scanner scanner; 
    private Number a; 
    private Number b; 

    public static void main(String args[]) throws Exception { 
     LargestOfTwo app = new LargestOfTwo(); 
     app.start(); 
    } 

    private void start() { 
     readInputs(); 
     Number largest = compare(a, b); 
     System.out.print("Largest of the numbers is: " + largest); 
    } 

    private void readInputs() { 
     scanner = new Scanner(System.in); 
     System.out.println("Enter two numbers, and I will show you which one is largest\n"); 
     a = readInput(); 
     b = readInput(); 
    } 

    private Number readInput() { 
     Double d = scanner.nextDouble(); 
     if (d == Math.floor(d)) { 
      return d.intValue(); 
     } else { 
      return d; 
     } 
    } 

    private Number compare(Number x, Number y) { 
     if (new BigDecimal(x.floatValue()).compareTo(new BigDecimal(y.floatValue())) > 0) { 
      return x; 
     } else { 
      return y; 
     } 
    } 
} 
를 찾을 수없는
+0

omg, 고마워요, 프로그램을 실행할 수는 없었지만 계속 진행되었습니다. 감사합니다 .. – JoeTinnySpace

+0

http://imgur.com/a/lP9is 알아낼 수 없기 때문에 지금 문제가 있습니다. 왜 내가이 일을 내 과제에 대해 제출할 것인가?이 논리는 논리가 강하다. :) – JoeTinnySpace

+0

코드에 닫는 중괄호가없는 것 같습니다 –

관련 문제