public static void sendMessage(String sender){
Scanner scTwo = new Scanner(System.in);
System.out.println("Enter your recipient: ");
String recipient = scTwo.next();
System.out.println("Enter your message: ");
String message = scTwo.next();
Message myMessage = new Message(sender, message);
}
public static void readMessage(Mailbox myMail){
for (int x = 0; x < myMail.getMail().size(); x++){
System.out.println("Email Number: " + x + " from " + myMail.getMail().get(x).getSender());
System.out.println("Message: " + myMail.getMail().get(x).getText());
}
}
public static void readUserName(String userName){
ArrayList<Mailbox> users = new ArrayList<Mailbox>();
for(int x = 0; x < users.size(); x++){
if (userName.equals(users.get(x))){
System.out.println("Hello " + userName + ".");
} else {
users.add(new Mailbox(userName));
System.out.println("Hello " + userName + ", welcome.");
}
}
}
public static void main(String[] args) {
boolean emailOn = true;
boolean done = false;
Scanner sc = new Scanner(System.in);
while (emailOn){
while(!done){
System.out.println("Enter username (-1 to quit): ");
if (sc.hasNextInt()){
String userName = sc.next();
readUserName(userName);
System.out.println("Your Options ");
System.out.println("(1) Read your email");
System.out.println("(2) Send an email");
System.out.println("(3) Log out of Email");
int number = sc.nextInt();
switch(number){
case 1 : readMessage(userName); break;
case 2 : sendMessage(userName); break;
case 3 : done = true; break;
}
//sc.next() = new Mailbox();
}
}
System.out.println("Logged out, have a nice day now!");
System.out.println("Enter \"-1\" to turn off.");
if(sc.nextInt() == -1){
emailOn = false;
} else {
emailOn = false;
}
}// ArrayList<Mailbox> mailboxes
System.out.println("Goodbye");
}
class Mailbox{
private ArrayList<Message> mail;
private String userNameTaken;
public Mailbox(String aUserName){
userNameTaken = aUserName;
}
public String getUserName(){
return userNameTaken;
}
public void addMessage(Message aMessage){
mail.add(aMessage);
}
public ArrayList<Message> getMail(){
return mail;
}
class Message{
private String sender;
private String text;
public Message(String aSender, String messageBody){
sender = aSender;
text = messageBody;
}
public String getText(){
return text;
}
public String getSender(){
return sender;
}
在案例1中我试图调用readMessage但是因为它是无效的,所以它将无效。我不确定如何解决这个问题。我是新手首先为用户调用ArrayList,然后调用消息来打印它们吗?我在readUserName中做了myitil的myitil。但是我无法将字符串转换为邮箱,因此readMessage(用户名)不适用。
答案 0 :(得分:2)
使用一个参数参数调用您的方法,
case 1 : readMessage(myMail); break;
注意:在此之前,您应该声明并保留myMail
作为您想要的内容