-1
對於我的程序,我需要以圖表形式顯示項目描述,數量和價格。這就意味着第1項的描述符合其價格和數量。到目前爲止,我已經嘗試了幾種在Internet上找到的方法,但都沒有成功。我正在使用Dr. Java,所以請建議一些與該編譯器兼容的東西。 提前謝謝!在Java中將字符串格式化爲圖表時最簡單但最有效的方法是什麼?
這是我到目前爲止有:
public static void main(String []args){
Scanner input=new Scanner(System.in);
String sentinel = "End";
String description[] = new String[100];
int quantity[] = new int[100];
double price [] = new double[100];
int i = 0;
// do loop to get input from user until user enters sentinel to terminate data entry
do
{
System.out.println("Enter the Product Description or " + sentinel + " to stop");
description[i] = input.next();
// If user input is not the sentinal then get the quantity and price and increase the index
if (!(description[i].equalsIgnoreCase(sentinel))) {
System.out.println("Enter the Quantity");
quantity[i] = input.nextInt();
System.out.println("Enter the Product Price ");
price[i] = input.nextDouble();
}
i++;
} while (!(description[i-1].equalsIgnoreCase(sentinel)));
// companyArt();
//System.out.print(invoiceDate());
//System.out.println(randNum());
System.out.println("Item Description: ");
System.out.println("-------------------");
for(int a = 0; a <description.length; a++){
if(description[a]!=null){
System.out.println(description[a]);
}
}
System.out.println("-------------------\n");
System.out.println("Quantity:");
System.out.println("-------------------");
for(int b = 0; b <quantity.length; b++){
if(quantity[b]!=0){
System.out.println(quantity[b]);
}
}
System.out.println("-------------------\n");
System.out.println("Price:");
System.out.println("-------------------");
for(int c = 0; c <price.length; c++){
if(price[c]!=0){
System.out.println("$"+price[c]);
}
}
System.out.println("-------------------");
//This is where I multiply the price and quantity together to get the total
double total = 0.0;
for (int j = 0; j < quantity.length; j++){
total += quantity[j] * price[j];
}
if(total != 0){
System.out.println("Total: " + total);
}
}
}
使用格式化程序:) –
*「我正在使用Dr. Java,所以請提供與該編譯程序兼容的東西。」* - 編譯器無關緊要。 –
你能舉一個你想要的輸出的例子嗎?我們可以嘗試猜測,但我們爲什麼要這樣做。你需要我們的幫助,所以請具體說明你需要什麼。以及您遇到麻煩的部分。 – Andreas