【发布时间】:2016-05-15 04:36:32
【问题描述】:
对于我的程序,我需要以类似图表的格式显示商品描述、数量和价格。所以这意味着项目 1 的描述将与其价格和数量一致。到目前为止,我已经尝试了几种我在互联网上找到的方法,但都没有成功。我正在使用 Dr. 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,所以请推荐与该编译器兼容的东西。” - 编译器无关紧要。
-
你能举一个你想要的输出的例子吗?我们可以尝试猜测,但我们为什么要猜测。您需要我们的帮助,所以请具体说明您的需要。以及您遇到问题的部分。
-
“到目前为止,我已经尝试了几种在网上找到的方法,但都没有成功。” - 请说明您遇到的问题。这个问题离“太宽泛”不远了。
标签: java string formatting output