Java以列/行显示输出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java以列/行显示输出相关的知识,希望对你有一定的参考价值。
我正在尝试将输出显示为列,而不是我现在得到的内容,而且它们到处都是。我希望它看起来像这样:
帐户“帐户1”:
0(1) 16 16,
1(0) 12 12,
2(2) 0 0
我已经尝试使用 t作为分隔符和printf,但无法弄明白。如果代码格式不正确,我很抱歉,我仍然很擅长编码
我感谢任何帮助或提示
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(" <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(" <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+")"+" "+" "+balance+" "+ balance+"
");
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+")"+" "+balance+" "+" "+ balance+"
");
cardholder++;
}
// Print out the statement of transactions
public static void printStatement() {
System.out.printf("%s
", "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");
}
}
你的主要问题是你以不同的方式推出了存款和取款方式。
在存款中你有:
array.add(array.size()+"("+cardholder+")"+" "+" "+balance+" "+ balance+"
");
退出:
array.add(array.size()+"("+cardholder+")"+" "+balance+" "+" "+ balance+"
");
因此,再次检查您想要多少,并确保在两种方法中使用相同的方式。
一般来说很少。正如@Kevin在上面的评论中所说,你应该在某些地方使用this.balance而不是平衡,因为你会得到错误的结果。
最后,如果你想在新行中打印所有没有逗号的Arraylist,你可以使用(从Java 8开始)
array.forEach(System.out::print);
array是列表的名称。还要确保导入Arrays包以使用这种方式。
import java.util.Arrays;
当然,如果你不喜欢这种方式,你也可以使用一个简单的for循环:
for (int i=0; i<array.size();i++){
System.out.print(array.get(i));
}
然后,您将结果作为列,并以相同的方式格式化。
以上是关于Java以列/行显示输出的主要内容,如果未能解决你的问题,请参考以下文章
片段(Java) | 机试题+算法思路+考点+代码解析 2023
在 Android 片段中获取 Java.Lang.NullPointerException
java缓冲字符字节输入输出流:java.io.BufferedReaderjava.io.BufferedWriterjava.io.BufferedInputStreamjava.io.(代码片段