2014-10-09 77 views
0

我目前正在学计算机科学,我有点卡在我的实验室。 下面列出了说明,我们需要使用OOP。 问题是我得到的输出超级奇怪,我真的不知道发生了什么。Java-OOP我一直在得到这个奇怪的输出

我运行该文件DiscountRunner.java(代码如下所示)后得到的输出是:

Enter the original bill amount :: 4000 
[email protected] 

为什么我不断得到[email protected]一部分?

/**================================================================================================== 
* 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; 
    } 
    } 

回答

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; 
    } 
} 

让我知道,如果你没有得到的东西。 如果解决了您的问题,请接受答案。

1

你得到这一点,因为这行:

System.out.println(bill); 

要打印哪个调用默认toString()实施Discount(从Object继承)的对象。您可以覆盖toString()Discount以获得自定义表示,但我想您只是想在此删除此System.out语句。

2

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

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

0

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

0

每当你将一个对象传递给System.out.println它会执行它的toString方法,toString的默认实现会用它的hashCode打印ClassName。你可以重写toString方法。下面的代码snnipet添加到您的折扣类别,并根据您的期望

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