2012-01-03 21 views
0

我有一個測試文件,根據它我需要構建我的程序測試文件 下面。但是,我困惑於s1.showDetails(System.out);我從來沒有遇到過 參數System.out任何人都可以幫助。該怎麼辦?當我試圖編寫showDetails()時,編譯器寫入錯誤。我的學生代碼是在這之下謝謝你提前!System.out參數

import java.util.*; 
public class Q2 { 
    public static void main(String [] args) 
    { 
     // Start on section A 
     System.out.println("Question 2"); 
     System.out.println("Start on part A"); 
     Student s1 = new Student("John", "Smith", 42); 
     s1.showDetails(System.out); 
     Course cs = new Course("Computer science"); 
    } 
} 

public class Student { 
    private String name; 
    private String familyName; 
    private int moduleMark; 
    private int total; 
    protected Student(String name, String familyName, int moduleMark) 
    { 
     this.name = name; 
     this.familyName = familyName; 
     this.moduleMark = moduleMark; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public String getFamilyName() 
    { 
     return familyName; 
    } 

    public int getModuleMark() 
    { 
     return moduleMark; 
    } 

    public String showDetails() 
    { 
     return (this.name + " " + this.familyName + " " + moduleMark + total); 
     //print(name); 
    } 
} 
+0

您需要發佈你所得到的錯誤。另外,如果這是作業,那麼添加'作業'標記 – 2012-01-03 21:58:37

+0

線程「main」中的異常java.lang.Error:未解決的編譯問題: \t Student類型的showDetails()方法不適用於參數(PrintStream) \t at Q2.main(Q2.java:9) – 2012-01-03 21:59:44

+0

這不是作業 – 2012-01-03 22:00:18

回答

1

該錯誤幾乎描述了問題所在。

The method showDetails() in the type Student is not applicable for the arguments (PrintStream) at Q2.main(Q2.java:9)

程序試圖用System.out調用您的方法,這恰好是PrintStream。因此,改變你的showDetails到

public String showDetails(PrintStream out) { 
    out.println(this.name + " " + this.familyName + " " + moduleMark + total); 
} 

這使得測試儀(我假設有一個程序,測試你是否正確分配)給你任何的PrintStream,無論是System.out的或任何其他的PrintStream。

1

錯誤基本上意味着,編譯器不使用名稱找到方法showDetails這需要PrintStream類型的參數。您無需將System.out傳遞給showDetails()方法。下面是編寫showDetails()的正確方法。閱讀關於System.out

public void showDetails() 
{ 
    System.out.println(this.name + " " + this.familyName + " " + moduleMark + total); 
} 
6

System.out是像其他變量的變量。

System是一類

out是內部PrintStream類型的System一個publicstatic變量。所以,你可以用System.out

訪問它所以System.out.println(..)僅僅是一個PrintStream

println(..)函數的調用,所以你的函數應該是這樣的:

public String showDetails(PrintStream stream) { 
... 
} 
0

你的程序不能編譯。

您的主要使用名爲showDetails的方法將PrintStream作爲參數(System.out)。而且您只定義了一個名爲showDetails的方法,不帶參數。

看看doc of System.out。這是一個像任何其他類系統一樣的領域。事實上,它有點特別,因爲它是靜態的,但並不會改變遊戲那麼多......

因此,編寫一個正確的參數列表的方法,你會更接近。

public String showDetails(PrintStream stream) { 
//your code here 
} 

showDetails應該寫入傳入參數中的流。

當你在學習編程。嘗試to separate query from command。這是一個很好的原則:一個方法應該做一件事:或者使用參數對當前對象進行一些操作,或者回答關於當前對象狀態的查詢。不是都。 在這裏,你返回一個字符串,並需要一個流......你應該更好地將它分成兩個方法:一個獲取字符串,然後第二個調用該字符串的stream.write。

public String toString() { 
//your code here 
} 

public void showDetails(PrintStream stream) { 
//your code here 
} 
0

由於print從您的showDetails消息註釋掉有2個明顯的解決方案

  1. 並不意味着showDetails信息打印出任何東西(儘管名字),你應該只把它打印出來的主要方法

    System.out.println(s1.showDetails()); 
    
  2. showDetails消息應打印字符串任何PrintStream,然後你必須調整該方法的兩個簽名以及實施

    public String showDetails(PrintStream stream) { 
        stream.println(...); 
    }