2014-11-05 55 views
-1

首先我對不起,如果這是一個奇怪的,但我不完全知道我在編碼方面想做什麼。有沒有辦法打印一個變量,改變同一個變量並在列表中打印它們中的兩個?

爲了闡明我想要做什麼,我想知道一種方法來將receipt的值「保存」到列表/集合/數組中,然後通過確定receipt的值的過程並將其保存到再次列出,當我打印清單時,在每個打字員後面打印兩個不同的收據值。 可以說第一次確定receipt的值等於x,然後我將它保存到一個列表中,然後通過相同的值檢測過程receipt = y並將其添加到列表中。它會打印:x然後y。或者y和y?如果兩次打印新值receipt,我如何才能在海誓山盟之後打印出兩個不同的值?

import java.util.Scanner; 

public class TicketV005 
{ // start of public class 
public static void main (String [] args) 
{ // start of main 
    Scanner keyboard = new Scanner(System.in); 
    int printedTickets,ticketTotal; 
    String fareType, purchaseLoc; 
    String answer1 =("null"); 
    String receipt =("null"); 
    int zoneAmount = 0; 
    double price = 0; 
    int answer2 = 0; 

    System.out.print("How many tickets do you wish to buy?(Answer with a number please.) "); 
    ticketTotal = keyboard.nextInt(); 
    printedTickets = 0; 

    while (ticketTotal <= 0) 
    { 
     System.out.println("\nIncorrect input, try again."); 
     System.out.println("How many tickets do you wish to buy?(Answer with a number please.) "); 
     ticketTotal = keyboard.nextInt(); 
    } 

    while(ticketTotal !=printedTickets) 
    { 
     System.out.print("Welcome! Are you buying a reduced fare ticket?(y/n) "); 
     answer1 = keyboard.next(); 

     if (answer1.equals("y")) 
      fareType=("reduced fare"); 

     else if (answer1.equals("n")) 
      fareType=("standard fare"); 

     else 
      fareType=("null"); 

     while (fareType.equals("null")) 
     { 
      System.out.println("\nIncorrect input, try again."); 
      System.out.println("Welcome! Are you buying a reduced fare ticket?(y/n) "); 
      answer1 = keyboard.next(); 
     } 

     System.out.println("Would you like to purchase your ticket at 1. the automated services or at 2. the cashier? "); 
     answer2 = keyboard.nextInt(); 

     if (answer2 ==1) 
      purchaseLoc=("automated services"); 

     else if (answer2 ==2) 
      purchaseLoc=("cashier"); 

     else 
      purchaseLoc=("null"); 

     while (purchaseLoc.equals("null")) 
     { 
      System.out.println("\nIncorrect input, try again."); 
      System.out.println("Would you like to purchase your ticket at 1. the automated services or at 2. the cashier? "); 
      answer2 = keyboard.nextInt(); 
     } 

     System.out.println("How many zones will you be travelling? (1-3) "); 
     zoneAmount = keyboard.nextInt();  

     while (zoneAmount <= 0 || zoneAmount > 3) 
     { 
      System.out.println("\nIncorrect input, try again."); 
      System.out.println("How many zones will you be travelling? (1-3) "); 
      zoneAmount = keyboard.nextInt(); 
     } 

     //Start of reduced fare 
      if (answer1.equals("y") && answer2 == 1) 
      { // Start of automated services reduced fare 

       for (int i= 1 ; i <= 3 ; i++) 
       { 
        if (zoneAmount == i) 
        price=(10*i)+10; 
       } 
      } //end off automated services reduced fare 

      if (answer1.equals("y") && answer2 == 2) 
      { // Start of cashier reduced fare 

       for (int i= 1 ; i <=3 ; i++) 
       { 
        if (zoneAmount == i) 
        price=(14*i)+14; 
       } 

      } //End of cashier reduced fare 

     //End of reduced fare 

     //Start of standard fare 
      if (answer1.equals("n") && answer2==1) 
      { //Start of standard fare automated services 

       for (int i = 1; i <=3 ; i++) 
       { 
        if (zoneAmount ==i) 
        price=(18*i)+18; 
       } 

      } // end of standard fare automated servies 

      if (answer1.equals("n") && answer2==2) 
      { // Start of standard fares cashier 

       for (int i = 1; i <=3 ; i++) 
       { 
        if(zoneAmount == i) 
        price=(22*i)+22; 
       } 

      } // End of standard fares cashier 
     //End of standard fare 

     System.out.println(""); 

     receipt = (zoneAmount+" zone(s), "+ fareType+", bought at: "+ purchaseLoc+". Your price: "+price+" SEK."); 
     System.out.println(receipt); 
     printedTickets++; 

     System.out.println(""); //Empty line for when/if it repeats 

    } // end of while printedTickets is not equal to ticketTotal 



}// end of main 
}// end of public class 

編輯1:包括完整的代碼爲了清晰。 編輯2:更好的說明

回答

0

它看起來像你問了兩個問題。一個涉及你是否可以保存一個變量,另一個涉及如何將一堆事物聚合在一起。從你的代碼中不清楚你的目標是什麼。

在Java中,你聲明的東西,比如

String receipt 

是引用。這意味着他們會跟蹤一段數據。但是,其他的東西也可以跟蹤同一條數據。所以:

String receipt1 = "100EUR"; 
String receipt2 = receipt1; 

在上面的代碼中,有一個在與它「100EUR」記憶的只有一件事,都receipt1和receipt2正在尋找它。如果有東西出現,並修改其中一個

receipt1 = "200EUR"; 

另一個將不受影響。

System.out.println(receipt1); 
System.out.println(receipt2); 

// output would be 
     200EUR 
     100EUR 

在很多情況下,一旦有什麼東西被分配到通過=符號的引用,你可以想像,參考總是要訪問它,保持不變,直到下一次=符號使用將參考分配給指向別的東西。然而,在Java對象可以改變狀態,所以如果你有

List<String> myList = new ArrayList<String>(): 

,事情開始呼籲myList方法來修改其內容,那麼myList中引用會覺得它是指一些變化。參考本身並沒有改變,只是它指向的對象。

由於您可能正在試圖在創建它們後的某個地方跟蹤所有收據,因此我將該主題帶回了列表,並且該列表有一個add函數,可以很好地爲您執行此操作。

如果你想打印出清單的全部內容,那麼你可以有一個循環:

for(String line:myList) { 
    System.out.println(line); 
} 
+0

爲清晰起見添加了完整的代碼 – 2014-11-06 07:09:40

+0

另外,我在某處發現了一個關於在用戶建議使用Set函數的列表中發佈的帖子,而您認爲哪種方法最有效? – 2014-11-06 07:22:09

+0

它看起來像你想要記住每個收據價值的最後輸出。如果你使用一個列表,他們會被記住。一組將會記住它們,但是以該組選擇存儲它們的順序。即使您正在重新定義'收據',您存儲在列表中的收據將不會受到我參考解釋的影響。 – 2014-11-06 11:38:03

0

您可以使用StringBuilder來追加新的字符串。或者,您可以簡單地將拖曳字符串與+ -Operator連接起來,但這在執行時間方面效率並不高。

+0

StringBuilder在內部用於執行靜態連接 - 即讀取String a =「b」+「c」+「d」'stuff的代碼,所以通常是一個好主意。在飛行中使用+並不完美,但它不會殺死你,除非你負擔很重。這只是一個相當薄弱的做法。 – 2014-11-05 22:34:12

0

首先,你可以聲明字符串的ArrayList:

ArrayList<String> receipts = new ArrayList<>(); 

然後添加收據變量的值:

receipts.add(receipt); 

最後您可以打印ArrayList的所有元素

for (String s : receipts) 
    System.out.println(s); 
+0

這幾乎是我想要做的,但是一旦reciept值的值已經添加到ArrayList中,我可以使用該方法通過確定接收值的進程,然後將該接收的新值添加到ArrayList ,沒有將第一個收件人的價值添加到正在更改的列表中? 編輯:拼寫。 – 2014-11-06 07:13:46

+0

抱歉,我並不真正瞭解您需要什麼。如果你給我一個具體的例子,很樂意幫忙。 – SkyMaster 2014-11-06 07:48:30

0

我會使用StringBuilder,並不斷添加你想要什麼的循環,在循環的末尾添加一個新行到它:

public static void main(String[] args) 
{ 
    StringBuilder receipt; 
    //Other variable initializations 

此外,while循環大概可以當你只是需要打印的StringBuilder

for (int i = 0; i < ticketTotal; i++) 
{ 
    receipt.append(zoneAmount); 
    receipt.append(" zone(s), "); 
    receipt.append(fareType); 
    receipt.append(", bought at: "); 
    receipt.append(purchaseLoc); 
    receipt.append(". Your price: "); 
    receipt.append(price); 
    receipt.append(" SEK.\n"); 
} 

然後:

System.out.println(receipt.toString()); 
改爲一個for循環