2017-12-15 6 views
0

Ich versuche, die Ausgabe als Spalten anstelle von dem, was ich gerade bekomme, und sie sind überall. Ich möchte es so etwas wie folgt aussehen:Java-Anzeige-Ausgabe in Spalten/Zeilen

Account "Account1":

0 (1) 16 16,

1 (0) 12 12,

2 (2) 0 0

Ich habe versucht, \ t als Separatoren und printf, aber kann es nicht herausfinden. Entschuldigt, wenn der Code schlecht formatiert ist, bin ich noch ziemlich neu

zu Codierung

schätze ich Hilfe oder Tipps

Account.java

import java.util.ArrayList; 

public class Account { 
private static String name; 
private int balance; 
int Transactions; 

private static ArrayList<String> array = new ArrayList<String>(); // array list 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // Check to make sure program has been called with correct number of 
    // command line arguments 
    if (args.length != 3) { 
     System.err.println("Error: program should take exactly three command line arguments:"); 
     System.err.println("\t<No. of card holders> <main acct starting bal.> <backup acct. starting bal.>"); 
     System.exit(0); 
    } 
    // And then make sure that those args are all integers 
    try { 
     int numCards = Integer.parseInt(args[0]); 
     Account account = new Account("Account1", Integer.parseInt(args[1])); 

     // Your code to create and manage the threads should go here. 
     Thread[] threads = new Thread[numCards]; 
     for (int i = 0; i < numCards; i++) { 
      threads[i] = new Thread(new CardHolder(i, account)); 
      threads[i].start(); 
     } 
     for (int i = 0; i < numCards; i++) { 
      threads[i].join(); 
     } 
    } catch (Exception e) { 
     System.err.println("All three arguments should be integers"); 
     System.err.println("\t<No. of card holders> <main acct starting bal.> <backup acct. starting bal.>"); 
    } 
    printStatement(); 
} 

// Create an account - initalisation goes in here 
public Account(String name, int bal) { 
    Account.name = name; 
    this.balance = bal; 
} 

// Deposit <balance> into the account 
public synchronized void deposit(int cardholder, int balance) { 
    balance = balance + balance; 
    notify(); 
    array.add(array.size()+"("+cardholder+")"+"\t"+"\t"+balance+"\t"+ balance+"\n"); 
    cardholder++; 
} 

// Withdraw <balance> from the account 
public synchronized void withdraw(int cardholder, int balance) { 

    if (balance < balance) { // 
     try { 
      System.err.println("Not enough money"); 
      wait(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    balance = balance - balance; 
    array.add(array.size()+"("+cardholder+")"+"\t"+balance+"\t"+"\t"+ balance+"\n"); 
    cardholder++; 
} 


// Print out the statement of transactions 
public static void printStatement() { 
    System.out.printf("%s\n", "Account \"" + name + "\":"); // cant figure out how to arrange it into rows/columns 

    System.out.println(array); 
} 
} 

CardHolder.java

public class CardHolder implements Runnable { 
private int id; 
private Account account; 
final static int numIterations = 20; 

public CardHolder(int id, Account account) { 
    this.id = id; 
    this.account = account; 
} 

/* 
* run method is what is executed when you start a Thread that 
* is initialised with an instance of this class. 
* You will need to add code to keep track of local balance (cash 
* in hand) and report this when the thread completes. 
*/ 
public void run() { 
    for (int i = 0; i < numIterations; i++) { 
     // Generate a random amount from 1-10 
     int amount = (int)(Math.random()*10)+1; 
     // Then with 50/50 chance, either deposit or withdraw it 
     if (Math.random() > 0.5) { 
      account.withdraw(id, amount); 
     } else { 
      account.deposit(id, amount); 
     } 
     try { 
      Thread.sleep(200); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
    System.out.println("THREAD "+ id + " finished"); 

} 
} 
+0

Nur einen Kopf hoch in Ihrer 'Zurückziehen' (und anderen Orten) Methode verwenden Sie einfach die Variable' balance', wo Sie 'this.balance' verwenden wollten – Kevin

Antwort

0

Ihr Hauptproblem ist, dass Sie in eine Di eingepasst haben verschiedene Art und Weise die \ t in den Ein-und Auszahlungsmethoden.

In Anzahlung Sie haben:

array.add(array.size()+"("+cardholder+")"+"\t"+"\t"+balance+"\t"+ balance+"\n"); 

Und zurückzuziehen:

array.add(array.size()+"("+cardholder+")"+"\t"+balance+"\t"+"\t"+ balance+"\n"); 

Also noch einmal überprüfen, wie viele \ t möchten Sie tun haben und sicher sein, sie auf die gleiche Weise setzen in beide Methoden.

Wenige Dinge im Allgemeinen. Wie @Kevin in einem Kommentar oben sagte, sollten Sie stattdessen this.balance an einigen Stellen ausgleichen, weil Sie falsche Ergebnisse erhalten werden.

Schließlich, wenn Sie alle Arraylist ohne die Kommas in der neuen Zeile drucken möchten, können Sie verwenden (da Java 8)

array.forEach(System.out::print); 

Array der Name Ihrer Liste. Stellen Sie außerdem sicher, dass Sie das Array-Paket importieren, um auf diese Weise zu arbeiten.

import java.util.Arrays;

Natürlich, wenn Sie auf diese Weise nicht auch Sie eine einfache for-Schleife verwenden können:

for (int i=0; i<array.size();i++){ 
    System.out.print(array.get(i)); 
} 

Dann werden Sie Ihre Ergebnisse als Spalten und formatiert die gleiche Art und Weise .

+0

Got it, vielen Dank – ItsMe9322

Verwandte Themen