十二点前要,java高手帮帮忙了

2025-01-08 10:05:25
推荐回答(4个)
回答1:

不知道你的十二点是什么时候,希望能对你有所帮助。
你给的网站我上不去,只是按照你的意思编了一下,个人不太明白msgBoxSize这个变量有什么实际意义,所以没对其做什么。并把inbox及outbox改为Vector的对象,方便扩展为收件箱及发件箱存储多个信息的版本。

谢谢楼下,我上不去他的网站,所以不知具体要求。
程序我已改过,请多指教!谢谢!

package baiduzhidao;

import java.util.Vector;

public class Handphone {
/*
* 定义变量
*/
private int phoneNum = 0;
private Vector inbox;
private Vector outbox;
private boolean newInMesg = false;
private int msgBoxSize;

/*
* 构造方法
*/

public Handphone(int number, int msgBoxSize){
phoneNum = number;
this.msgBoxSize = msgBoxSize;
inbox = new Vector();
outbox = new Vector();
}
/*
* 发信方法
*/
public void sendMesg(Handphone receiver, String mesg){
String message = String.valueOf(receiver.getNum()) + ":" + mesg;
String sentMessage = String.valueOf(phoneNum) + ":" + mesg;
outbox.clear();
outbox.add("To " + message);
receiver.reMesg(sentMessage);
}
/*
* 收信方法,个人觉得应该此方法,保证变量安全
*/
public void reMesg(String mesg){
inbox.clear();
inbox.add("From " + mesg);
}

public int getNum(){
return phoneNum;
}
public boolean newIncoming(){
newInMesg = !inbox.isEmpty();

return newInMesg;
}
public void readInbox(){
for(int i = 0; i System.out.println(inbox.get(i));
}
}
public void readOutbox(){
for(int i = 0; i System.out.println(outbox.get(i));
}
}
public void clearMesg(){
inbox.clear();
outbox.clear();
}

public static void main(String[] args)
{
Handphone hp1 = new Handphone(91111111,1);
Handphone hp2 = new Handphone(92222222,2);
Handphone hp3 = new Handphone(93333333,3);

System.out.println("\nPCK(hp1) messages Rosie(hp2)");
hp1.sendMesg(hp2, "Hello Rosie ah!");

System.out.println("PCK's OUTBOX");
hp1.readOutbox();

if (hp2.newIncoming())
{
System.out.println("Rosie's INBOX");
hp2.readInbox();
}

System.out.println("\nRosie responds...");
hp2.sendMesg(hp1, "Kang Kang ah, What is it?");
if (hp1.newIncoming())
{
System.out.println("PCK's INBOX");
hp1.readInbox();
}
if (hp2.newIncoming())
{
System.out.println("Rosie's INBOX");
hp2.readInbox();
}

System.out.println("\nFrankie(hp3) messages Rosie");
hp3.sendMesg(hp2, "Rosie darling... Frankie here");
if (hp2.newIncoming())
{
System.out.println("Rosie's INBOX");
hp2.readInbox();
}

System.out.println("\nMore messages sent...");
hp1.sendMesg(hp2, "How come you not at home?");
hp2.sendMesg(hp3, "Aiyo Frankie, I busy lei... Don't mesg me lah!");
hp2.sendMesg(hp1, "Chu ah... can lend me money for mahjong or not?");

System.out.println("PCK's INBOX"); hp1.readInbox();
System.out.println("PCK's OUTBOX"); hp1.readOutbox();
System.out.println("Rosie's INBOX"); hp2.readInbox();
System.out.println("Rosie's OUTBOX"); hp2.readOutbox();
System.out.println("Frankie's INBOX"); hp3.readInbox();
System.out.println("Frankie's OUTBOX"); hp3.readOutbox();
}
}

回答2:

楼上的运行结果和网站给的例子结果不一样,网站例子结果如下:
Sample Run

The following output should be expected.

PCK(hp1) messages Rosie(hp2)
PCK's OUTBOX
To 92222222: Hello Rosie ah!
Rosie's INBOX
From 91111111: Hello Rosie ah!

Rosie responds...
PCK's INBOX
From 92222222: Kang Kang ah, What is it?

Frankie(hp3) messages Rosie
Rosie's INBOX
From 93333333: Rosie darling... Frankie here
From 91111111: Hello Rosie ah!

More messages sent...
PCK's INBOX
From 92222222: Chu ah... can lend me money for mahjong or not?
PCK's OUTBOX
To 92222222: How come you not at home?
Rosie's INBOX
From 91111111: How come you not at home?
From 93333333: Rosie darling... Frankie here
Rosie's OUTBOX
To 91111111: Chu ah... can lend me money for mahjong or not?
To 93333333: Aiyo Frankie, I busy lei... Don't mesg me lah!
Frankie's INBOX
From 92222222: Aiyo Frankie, I busy lei... Don't mesg me lah!
Frankie's OUTBOX
To 92222222: Rosie darling... Frankie here

回答3:

--------------------注释已经添加------------

public class Handphone {
private int phoneNum;
public String inbox;
public String outbox;
public boolean newInMesg;
public Handphone(int number,int mesgBoxSize) //initial the inbox,oubox and assign the phoneNum property
{
inbox="";
outbox="";
this.phoneNum=number;
newInMesg=false;
}
void sendMesg(Handphone receiver,String mesg) //send a message to specified personal.
{
receiver.newInMesg=true; //set the receiver's newInMesg with true.
receiver.inbox="From "+phoneNum+": "+mesg; //assign the receiver's inbox message.
outbox="To "+receiver.phoneNum+": "+mesg; //assign the sender's outbox.
}
boolean newIncoming() // determine weather there is a new phone.
{
return newInMesg;
}
void readInbox()
{
System.out.println(inbox); //display the latest new message.
}
void readOutbox()
{
System.out.println(outbox); //display the latest message has been sent.
}
void clearMesg()
{
inbox=""; //assign outbox with nothing.
outbox=""; //assign inbox with nothing.
newInMesg=false; //no new message exists.
phoneNum=0; //no number exists.
}
public static void main(String[] args)
{

Handphone hp1 = new Handphone(91111111,1);
Handphone hp2 = new Handphone(92222222,2);
Handphone hp3 = new Handphone(93333333,3);

System.out.println("\nPCK(hp1) messages Rosie(hp2)");
hp1.sendMesg(hp2, "Hello Rosie ah!");

System.out.println("PCK's OUTBOX");
hp1.readOutbox();

if (hp2.newIncoming())
{
System.out.println("Rosie's INBOX");
hp2.readInbox();
}

System.out.println("\nRosie responds...");
hp2.sendMesg(hp1, "Kang Kang ah, What is it?");
if (hp1.newIncoming())
{
System.out.println("PCK's INBOX");
hp1.readInbox();
}
if (hp2.newIncoming())
{
System.out.println("Rosie's INBOX");
hp2.readInbox();
}

System.out.println("\nFrankie(hp3) messages Rosie");
hp3.sendMesg(hp2, "Rosie darling... Frankie here");
if (hp2.newIncoming())
{
System.out.println("Rosie's INBOX");
hp2.readInbox();
}

System.out.println("\nMore messages sent...");
hp1.sendMesg(hp2, "How come you not at home?");
hp2.sendMesg(hp3, "Aiyo Frankie, I busy lei... Don't mesg me lah!");
hp2.sendMesg(hp1, "Chu ah... can lend me money for mahjong or not?");

System.out.println("PCK's INBOX"); hp1.readInbox();
System.out.println("PCK's OUTBOX"); hp1.readOutbox();
System.out.println("Rosie's INBOX"); hp2.readInbox();
System.out.println("Rosie's OUTBOX"); hp2.readOutbox();
System.out.println("Frankie's INBOX"); hp3.readInbox();
System.out.println("Frankie's OUTBOX"); hp3.readOutbox();
}
}

回答4: