【问题标题】:How to fix 'cannot find symbol' in a function call?如何修复函数调用中的“找不到符号”?
【发布时间】: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


【解决方案1】:
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;
        }
    }
}

【讨论】:

  • omg,谢谢,我无法运行程序,但仍然取得了进展,谢谢..
  • imgur.com/a/lP9is 这是现在的问题,因为我不知道为什么,我提交这个作为我的作业,这个逻辑很强大。 :)
  • 另外,我的代码创建了一个 LargestOfTwo 实例,其中包含扫描仪字段和两个数字。这些是在方法 readInputs() 中分配的。您复制了方法,但没有复制必填字段,因此无法正常工作。
猜你喜欢
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
相关资源
最近更新 更多