Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
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!");
		}
	}
}


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.
Posted
Updated 4-Oct-18 6:37am
v2

1 solution

Your while loop is stopping too early. Each iteration should collect all the values for a year and print the totals. Move the closing brace after the following if/else block.
 
Share this answer
 
Comments
Maciej Los 4-Oct-18 13:34pm    
5ed!
Richard MacCutchan 4-Oct-18 15:09pm    
Thanks Maciej.
Member 14007867 4-Oct-18 17:15pm    
Thank you so much!! I truly appreciate it!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900