import java.util.Scanner;
public class SellingPrice {
public static void main (String [] agrs) {
Scanner input = new Scanner (System.in);
int salesRevenue = 0;
int productsSold = 0;
System.out.println();
System.out.print("Please press -1 + Enter two times to display the calculation!");
System.out.println();
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;
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();
}
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!");
}
}
}
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.