Java-OOP 我不断收到这种奇怪的输出

标签 java eclipse oop computer-science bluej

我目前正在学校学习计算机科学,但我有点困在我的实验室里。 下面列出了说明,我们需要使用 OOP。 问题是我得到的输出非常奇怪,我真的不知道发生了什么。

运行文件 DiscountRunner.java (下面列出的代码)后得到的输出是:

Enter the original bill amount :: 4000
Discount@1d1be4e

为什么我不断获得 Discount@1d1be4e 部分?

/**==================================================================================================
 * Objective :         This lab was designed to teach you how to use if statements.
 * Lab Description :   Determine the amount of discount a person should receive.   
 *                     If the bill is more than 2000, then the person should receive a 15% discount.  
 *                     If the bill is 2000 dollars or less, the person does not receive a 
 *                     discount of any kind.
 ===================================================================================================*/


import static java.lang.System.*;
import java.util.Scanner;

public class DiscountRunner
{
public static void main( String [] args )
{
    Scanner keyboard = new Scanner(System.in);

    out.print("Enter the original bill amount :: ");
    int amt = keyboard.nextInt();

    int Discount;

    Discount bill=new Discount();
    bill.getDiscountedBill();

    System.out.println(bill);
    //instantiate a new Discount object called "bill"
    //print the output of bill.getDiscountedBill() with amt as the parameter

}
}

这是文件一。

这是文件二。

import static java.lang.System.*;
import java.util.Scanner;

public class Discount
{
//instance variables and constructors could be used, but are not really needed

int amt;
int bill;

//getDiscountedBill() will return final amount of the bill
//          if the bill is >2000, the bill receives a 15% discount



   public int getDiscountedBill()
   {
       if ( amt>2000)
           bill=amt*(int).85;
       if ( amt<=2000)
           bill=amt*1;


       return bill;
   }
  }

最佳答案

System.out.println() 方法将在打印之前尝试将对象转换为字符串。为了做到这一点,它通常会调用 Object.toString() 方法。该方法的默认实现是生成一个包含对象类型和对象内存地址的字符串。这就是发生在你身上的事情。

要修复此问题,您需要为 Discount 方法提供 toString() 实现。

关于Java-OOP 我不断收到这种奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26271282/

相关文章:

Java 泛型有声明

android - 如何在eclipse中为android项目集成aar库依赖

java - 在 Eclipse 中使用 Android build.xml?

java - class is-a-JPanel 当它应该有-a

java - 在比较内容之前如何首先验证两个文件名?

java - 为什么在我启动服务器时 Tomcat 不启动连接器?

java - 位图 getWidth 返回错误值

java - 在eclipse中运行时找不到类异常

java - 用于文本片段和类实例双向映射的数据模型

java - 将父类(super class)传递到需要子类的方法中