2014-03-28 55 views
0

我想爲明信片製作一個類和一個單獨的打印機類。這個想法是製作一張明信片,可以爲發件人,收件人和場合提供用戶輸入。然後添加一些東西,使我們可以將相同的明信片發送給另一位朋友。這是我的明信片類Java明信片類和打印機

public class Postcard 
    { 
     private String message; 
     //define other variables that you need in this class 
     private String sender; 
     private String recipiant; 
     private String occasion; 
     private String print; 
     // Methods go here 
     public Postcard() 
     { 
      String message = "Happy holidays too "; 
      String sender = "Michael"; 
      String recipiant = ""; 
      String occasion = ""; 
     } 
     public void setmessage(String m) 
     { 
      this.message = m; 
     } 
     public void setSender(String s) 
     { 
      this.sender = s; 
     } 
     public void setRecipiant(String r) 
     { 
      this.recipiant = r; 
     } 
     public void setOccasion(String o) 
     { 
      this.occasion = o; 
     } 
     public String print() 
    { 
      print = message + sender + recipiant + occasion; 
      return print; 
    } 
} 

,這是明信片印刷類

import java.util.Scanner; 
public class PostcardPrinter 
{ 
    public static void main(String[] args) 
    { 
     String text = "Happy Holiday to ";//write your msg here 
     Postcard myPostcard = new Postcard(); // use the constructor method 
     //use the mutator method to set the name of the recipient 
     Scanner op = new Scanner(System.in); 
     String recipant = op.nextLine(); 
     String sender = op.nextLine(); 
     String occassion = op.nextLine(); 


     myPostcard.print(); 

     //write the code to send the same msg to another friend 
     System.out.println("Do you want to send another? Type 'yes' or 'no' "); 
     String choice = op.nextLine(); 
     while (choice != no) 
     { 
     String text = "Happy Holiday to "; 
     Postcard myPostcard = new Postcard(); 
     Scanner op = new Scanner(System.in); 
     String recipant = op.nextLine(); 
     String sender = op.nextLine(); 
     String occassion = op.nextLine(); 
     } 
    } 
    } 

錯誤的出現在while循環中說,varriable沒有不存在,當註釋掉,什麼都不會發生。虛擬機正在運行,但沒有任何反應。任何幫助將不勝感激

+0

似乎是* Java *問題,而不是* JavaScript *。重新標記。 –

回答

3

行:

while (choice != no) 

正在尋找可變稱爲no,而不是一個字符串常量。你想:

while (!choice.equals("no")) 

或者,案例insenstive方法:

while (!choice.equalsIgnoreCase("no")) 

有一點需要指出的 - 因爲choice值從不內循環改變一次,你就基本上可以無休止的循環下去。你可能會想要在每次循環迭代後再次詢問。您可以將初始值選項設置爲空字符串,然後在程序開始時立即啓動循環。這將允許您刪除循環上方的冗餘代碼。