2017-12-15 3 views
0

현재 출력하고있는 것 대신에 출력을 표시하려고하고 있습니다.Java 디스플레이 출력을 열/행

계정 "계정 1을": 나는 그것이 어느 정도 같이 할

0 (1) (16) (16),

1 (0) 12 12,

2 (2) 0 0

\ t를 구분 기호와 printf로 사용해 보았지만 알아 내지 못했습니다. 코드가 제대로 포맷 사과 경우, 나는 아직도 내가 어떤 도움이나 조언

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

Account.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

'withdraw'(및 다른 장소) 방법에서 그냥'this.balance'를 사용하려는 'balance'변수를 사용하십시오. – Kevin

답변

0

귀하의 주요 문제는 귀하가 di 다른 방법으로 입금 및 인출 방법에 \ t. 보증금에서

당신은 :

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

과의

철회 :

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

그래서 다시 한 번 당신이하고 그들에게 같은 방법으로 넣어해야할까요 얼마나 많은 \의 t 확인 두 가지 방법.

몇 가지가 일반적입니다. @ Kevin은 위의 설명에서 this.balance 대신에 잘못된 결과가 표시되기 때문에 일부 지역에서는 균형을 사용해야한다고 말했습니다. 새 라인의 쉼표없이 모든 ArrayList를 인쇄하려면

마지막으로, 당신은

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

배열이 목록의 이름입니다 (자바 8 년 이후)을 사용할 수 있습니다. 또한 Array 패키지를 가져 와서 그런 식으로 사용해야합니다. 물론

import java.util.Arrays;

, 당신은 당신이뿐만 아니라 간단한 for 루프를 사용할 수 있습니다 이런 식으로 마음에 들지 않는 경우 :

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

는 그런 다음 열로 결과를해야합니다와 같은 방식으로 형식 .

+0

정말 고마워요. – ItsMe9322

관련 문제