Java-OOP我继续得到这个奇怪的输出

时间:2014-10-09 05:57:28

标签: 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;
   }
  }

5 个答案:

答案 0 :(得分:2)

System.out.println()方法会尝试在将对象打印出来之前将其转换为字符串。为此,通常会调用Object.toString()方法。所述方法的默认实现是生成包含与对象的内存地址连接的对象类型的字符串。这就是你发生的事情。

要解决此问题,您需要为toString()方法提供Discount实施。

答案 1 :(得分:1)

由于这一行,你得到了这个:

System.out.println(bill);

您正在打印调用Discount的默认toString()实现的对象(继承自Object)。您可以在toString()中覆盖Discount以获取自定义表示形式,但我想您只是想在那里删除此System.out语句。

答案 2 :(得分:0)

您没有覆盖Java类toString()继承的Discount类中的Object方法。这会导致您列出的输出。

答案 3 :(得分:0)

每当您将Object传递给System.out.println时,它都会执行toString方法,toString的默认实现会使用hashCode打印ClassName。你可以覆盖toString方法。将以下代码snnipet添加到您的折扣级别,它将按照您的期望运行

@Override
public String toString()
{
    return "Bill Amount After Discount is " + bill ;
}

答案 4 :(得分:0)

你有类型转换的一些小错误,有些从方法接收值。 在下面找到工作代码

import static java.lang.System.*;

import java.util.Scanner;

public class DiscountRunner
{
public static void main( String [] args )
{
    Scanner keyboard = new Scanner(System.in);
    Discount bill=new Discount();
    out.print("Enter the original bill amount :: ");
    bill.amt = keyboard.nextInt();

    int discount;


    discount=bill.getDiscountedBill();

    System.out.println(discount);
    //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=(int)(amt*.85);**
       if ( amt<=2000)
           bill=amt*1;

       return bill;
   }
}

如果你没有得到什么,请告诉我。 如果它解决了你的问题,请接受答案。