2014-10-09 50 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 ; 
}