我目前正在学校学习计算机科学,但我有点困在我的实验室里。 下面列出了说明,我们需要使用 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/