5 Replies - 434 Views - Last Post: 23 October 2013 - 07:26 PM Rate Topic: -----

#1 Highstandard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-October 13

Updated Java Homework Question

Posted 23 October 2013 - 06:53 PM

A mail-order house sells five products whose retail prices are as follows: product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49 and product 5, $6.87. Write an application that reads a series of pairs of numbers as follows:
a) product number
B)/> quantity sold
Your program should use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.

This is the code I have so far the problem is it doesn't run all the way through any suggestions?

import java.util.Scanner;


public class Retail
{

	public static void main (String[] args){

		//Declare Variable
		int product;
		int quantity;
		int totalretail = 0;
		
		double product1 = 2.98;
		double product2 = 4.50;
		double product3 = 9.98;
		double product4 = 4.49;
		double product5 = 6.87;
	
		//Creating a Scanner object to read input
		Scanner keyboard = new Scanner(System.in);
		
		//Multiplying product price by quantity.
			System.out.println("Enter quantity sold:");
		quantity = keyboard.nextInt();
		
		System.out.println("Enter product number (1-5 or 0 to stop):");
		product = keyboard.nextInt();

		
		while (product >= 6)
		
		switch(totalretail){
		
		case 1:
		
		totalretail+= product1*quantity;
			
		case 2:
			totalretail += product2*quantity;
		

		case 3:
			totalretail += product3*quantity;
			
		case 4:
			totalretail += product4*quantity;
		

		case 5:
			totalretail += product5*quantity;
			if (product != -1){

		System.out.println("Total retail value of all products sold is:" + totalretail);
		{
}

*** EDIT ***
Please use code tags when posting code.
:code:

This post has been edited by GunnerInc: 23 October 2013 - 07:27 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Updated Java Homework Question

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,696
  • Joined: 19-March 11

Re: Updated Java Homework Question

Posted 23 October 2013 - 06:55 PM

I suggest you start by posting in the right forum. javascript != java...

(don't worry, it will be moved to the java forum presently)

Second suggestion: use the code tags so your code shows up formatted correctly.

Like this:

public static void main(String[] args) {
  System.out.println("Hello, world!");
}



(quote and reply to this post to see what that looks like in the editor)


Now, to answer your actual question:

while (product >= 6)



What is the value of product when you first get to this line? If the user has obeyed instructions, then it's less than 6, so you won't enter the loop. Try using <= instead.

Also, you need to use breaks in your cast statements - if you don't, you'll "fall through" to the end of the switch.

i = 1;
switch (i)
{
  case (1): i +=1;
  case (2): i +=1;
  case (3): i +=1;
  case (4): i +=1;
  default: i *=5;
}



Suppose i is in the range 1..5, inclusive. What is the value of i after this runs? (test your answer, please - when the computer will give you the right answer every time, there's no excuse for not checking it AFTER you've come up with an answer of your own)

This post has been edited by jon.kiparsky: 23 October 2013 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 Highstandard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-October 13

Re: Updated Java Homework Question

Posted 23 October 2013 - 07:02 PM

A mail-order house sells five products whose retail prices are as follows: product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49 and product 5, $6.87. Write an application that reads a series of pairs of numbers as follows:
a )product number
b ) quantity sold
Your program should use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.

The print out should resemble something like this.


Enter product number (1-5 or 0 to stop): 6
Product number must be between 1 and 5 or 0 to stop
Enter product number (1-5 or 0 to stop): 1
Enter quantity sold: 10
Enter product number (1-5 or 0 to stop): 3
Enter quantity sold: 20
Enter product number (1-5 or 0 to stop): 5
Enter quantity sold: 30
Enter product number (1-5 or 0 to stop): 0

Total retail value of all products sold is: 435.5

import java.util.Scanner;

public class RetailSales
{

	public static void main (String[] args){

		//Declare Variable
		int product;
		int quantity;
		int totalretail = 0;
		
		double product1 = 2.98;
		double product2 = 4.50;
		double product3 = 9.98;
		double product4 = 4.49;
		double product5 = 6.87;
		
		

		//Creating a Scanner object to read input
		Scanner keyboard = new Scanner(System.in);
		
		//Multiplying product price by quantity.
		

		System.out.println("Enter quantity sold:");
		quantity = keyboard.nextInt();
		
		System.out.println("Enter product number (1-5 or 0 to stop):");
		product = keyboard.nextInt();

		
		while (product >= 6)
		
		switch(totalretail){
		
		case 1:
		
		totalretail+= product1*quantity;
			
		case 2:
			totalretail += product2*quantity;
		

		case 3:
			totalretail += product3*quantity;
			
		case 4:
			totalretail += product4*quantity;
		

		case 5:
			totalretail += product5*quantity;
			if (product != -1){

		System.out.println("Total retail value of all products sold is:" + totalretail);
		{
}

	





*** EDIT ***
Original post...
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,696
  • Joined: 19-March 11

Re: Updated Java Homework Question

Posted 23 October 2013 - 07:04 PM

Please don't create duplicate threads. Thanks!

This post has been edited by jon.kiparsky: 23 October 2013 - 07:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 Highstandard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-October 13

Re: Updated Java Homework Question

Posted 23 October 2013 - 07:08 PM

Didn't know how to edit the first one kinda new to this website.
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: Updated Java Homework Question

Posted 23 October 2013 - 07:26 PM

First we will move both of your threads out of the Javascript forum, and into the Java forum. Next we will merge both of your threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1