0
這裏是Message
類我創建:如何調試Java消息輸出始終爲空的原因?
public class Message {
static String toString;
static String sender;
static String recipient;
static String messageBody ;
public Message(String Sender, String Recipient){
sender = Sender ;
recipient = Recipient ;
messageBody = "";
}
public String getSender(String s){
sender = s;
return sender;
}
public String getRecipient(String r){
recipient = r;
return recipient;
}
public void append (String a){
a = messageBody +a;
}
public void toString (String m){
messageBody = m;
m = ("From: " + sender + "\nTo: "+ recipient+"\n" + "\n" + messageBody);
System.out.print(m);
}
}
以下是main
方法。我需要使用toString()
方法打印。
import java.util.Scanner;
public class Lab8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Message mess1 = new Message (null, null);
Scanner keyboard = new Scanner (System.in);
String Sender1= Message.sender;
String recipient1 = Message.recipient;
String message = Message.messageBody ;
System.out.println ("Input Sender Name: ");
Sender1 = keyboard.next();
System.out.println ("Input Recipent Name: ");
recipient1 = keyboard.next();
do {
System.out.print ("Input Message: ");
message = keyboard.next();
} while (!message.equals("***"));
System.out.print(Message.toString);
}
}
每次打印null
。我還需要包含append方法,該方法允許我在每行之後更改郵件正文。
雖然這編譯和執行,這裏有嚴重的問題。最大的問題是,當你真的需要實例字段時,Message使用'static'字段:從Message類中刪除'static'。 – Bohemian
我有,然後我設置一個返回變量,我仍然有同樣的問題。 – Cotypiechocki