如何按顺序打印年份声明? [英] How do I print the year statement in order?
问题描述
import java.util.Scanner; // program uses class Scanner
public class SellingPrice {
public static void main (String [] agrs) {
//create Scanner object to read inputs
Scanner input = new Scanner (System.in);
//initialization phase
int salesRevenue = 0; //initialize sales revenue
int productsSold = 0; //initialize # of products sold
System.out.println();
System.out.print("Please press -1 + Enter two times to display the calculation!");
System.out.println();
//prompt for input - sales revenue and # of product
System.out.printf("Enter the sales revenue for year or -1 to quit: ");
int saleNumbers = input.nextInt();
System.out.print("Enter a total number of products sold or -1 to quit: ");
int productNumbers = input.nextInt();
while (saleNumbers != -1 && productNumbers != -1){
salesRevenue = salesRevenue + saleNumbers;
productsSold = productsSold + productNumbers;
//prompt for another input
System.out.print("Enter the sales revenue for each year or -1 to quit: ");
saleNumbers = input.nextInt();
System.out.print("Enter a total number of products sold or -1 to quit: ");
productNumbers = input.nextInt();
}//end while
if (salesRevenue != 0 && productsSold != 0){
double totalPrice = ((double) salesRevenue / productsSold);
System.out.printf("Selling price for the first year is $ %d%n", saleNumbers);
System.out.printf("Total sales revenue is $%d%n", salesRevenue);
System.out.printf("Total number of products sold is %d%n", productsSold);
System.out.printf("Combined selling price is $%.2f%n", totalPrice);
}
else {
System.out.print("No inputs were entered!");
}
}
}
这是我的Java代码到目前为止,我的问题是我不知道如何按照我的预期显示年份。我让其他的东西工作得很好,但我不能显示年份(第1年和第2年)。我被允许只使用For循环和While循环,没有数组。请帮我。
这应该是结果
例如;第一年销售收入为1000,产品总销售数量为10,第二年销售收入为2000,销售产品总数为10)(我们假设用户输入两个输入)
打印:
第一年的销售价格是$ 100.00(1000/10)
第二年的销售价格为$ 200.00(2000/10)
总销售收入为$ 3000.00(1000 + 2000)
销售的产品总数为20.00( 10 + 10)
合并售价为$ 150.00(3000/20)
我的尝试:
我尝试使用不同的循环和字面上的一切,我重写了很多次。我现在很困惑。我只允许使用For循环,While循环和Sentinel循环。
This is my Java code so far, my problem is I don't know how to display the year as I expected. I got the other things work just fine but I can't display the year (1st year and 2nd year). I'm allowed to use For loop and While loop only, no arrays. Please help me.
This should be the result
For example; for the first-year sales revenue is 1000, the total number of products sold 10, for the second-year sales revenue is 2000, the total number of products sold 10) (we are assuming that user enters two inputs)
It will print:
Selling price for the first year is $100.00 (1000/10)
Selling price for the second year is $200.00 (2000/10)
Total sales revenue is $3000.00 (1000+2000)
Total number of products sold is 20.00 (10+10)
Combined selling price is $150.00 (3000/20)
What I have tried:
I tried using different loops and literally everything, I rewrote everything a bunch of times. I'm confusing right now. I'm allowed to use For loop, While loop and Sentinel loop only.
推荐答案
%d%n,saleNumbers);
System.out.printf( 总销售收入
%d%n", saleNumbers); System.out.printf("Total sales revenue is
%d%n ,salesRevenue);
System.out.printf( 销售的产品总数为%d%n,productsSold);
System.out.printf( 组合销售价格是
%d%n", salesRevenue); System.out.printf("Total number of products sold is %d%n", productsSold); System.out.printf("Combined selling price is
%。2f%n,totalPrice);
}
else {
System.out.print( 未输入任何输入!);
}
}
}
%.2f%n", totalPrice); } else { System.out.print("No inputs were entered!"); } } }
这是我的Java代码到目前为止,我的问题是我不知道我不知道如何按照我的预期来展示这一年。我得到了其他的工作就好了,但我无法显示年份(第1年和第2年)。我被允许只使用For循环和While循环,没有数组。请帮我。
这应该是结果
例如;第一年销售收入为1000,产品总销售数量为10,第二年销售收入为2000,销售产品总数为10)(我们假设用户输入两个输入)
打印:
第一年的销售价格是
This is my Java code so far, my problem is I don't know how to display the year as I expected. I got the other things work just fine but I can't display the year (1st year and 2nd year). I'm allowed to use For loop and While loop only, no arrays. Please help me.
This should be the result
For example; for the first-year sales revenue is 1000, the total number of products sold 10, for the second-year sales revenue is 2000, the total number of products sold 10) (we are assuming that user enters two inputs)
It will print:
Selling price for the first year is
这篇关于如何按顺序打印年份声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!