【发布时间】:2017-03-27 13:34:57
【问题描述】:
我正在使用 2 种不同的数据类型实现方法重载。这就是我最终得到代码的方式。 但现在它找不到符号 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)
【问题讨论】:
-
您已声明
c和d超出了您调用largest的范围,这与重载无关。 -
在一个范围内,你只能有一个变量声明。您尝试通过将声明放在
if中来创建不同的声明是行不通的,因为if块是单独的范围,一旦块完成,声明就会超出范围。 -
您应该改写您的标题以针对特定问题,否则您将继续被否决。只是一个友好的提醒。
-
嗯明白了,谢谢大家
-
java 中是否有数据类型,可以同时存储 integer 和 double 。 P.S 该程序是我的作业,我刚开始练习 Java,请随时提供帮助。
标签: java overloading