473,387 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stuck with Switch

I am a new member.
I got stuck using Switch in java. Could someone help me?

This is my code:
Thanks




Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class MailOrder { 
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.       Scanner input = new Scanner( System.in );//declare Scanner object
  8.  
  9.       int productID=0;//productID
  10.       double totalValue = 0; //total value of the order
  11.       double Product1 = 2.98;
  12.       double Product2 = 4.50;
  13.       double Product3 = 6.87;
  14.  
  15.       System.out.println("Enter product number (1-3): " );
  16.       productID = input.nextInt(); //read the product ID
  17.  
  18.       // determine the number sold of the item
  19.       System.out.println( "Enter quantity ordered: " );
  20.       int quantity = input.nextInt();
  21.  
  22.       //please complete the switch statement
  23.       switch (productID)
  24.       {
  25.           case 1:                     
  26.              quantity += quantity;
  27.              break;
  28.  
  29.           case 2:                       
  30.              quantity += quantity;
  31.              break;
  32.  
  33.           case 3:                     
  34.              quantity += quantity;
  35.              break;
  36.  
  37.          } // end switch
  38.  
  39.          if (totalValue !=0)
  40.              System.out.println ("Total Valude of this order is "+ totalValue);
  41.         }
  42.  
  43. }
Oct 3 '07 #1
11 1535
Ganon11
3,652 Expert 2GB
What are you having trouble with?
Oct 4 '07 #2
Thanks for quick reply.

I do not know how to set up the formula. When I run the file, it looks like this:

init:
deps-jar:
compile-single:
run-single:
Enter product number (1-3):



When I enter information, it becomes this:


init:
deps-jar:
compile-single:
run-single:
Enter product number (1-3):
1
Enter quantity ordered:
3
BUILD SUCCESSFUL (total time: 6 minutes 31 seconds)



Can you help how to figure out?
Many thanks.
Oct 4 '07 #3
madhoriya22
252 100+
Thanks for quick reply.

I do not know how to set up the formula. When I run the file, it looks like this:

init:
deps-jar:
compile-single:
run-single:
Enter product number (1-3):


When I enter information, it becomes this:


init:
deps-jar:
compile-single:
run-single:
Enter product number (1-3):
1
Enter quantity ordered:
3
BUILD SUCCESSFUL (total time: 6 minutes 31 seconds)


Can you help how to figure out?
Many thanks.
Hi,
Can tell us what are you trying to do with ur program ? And how come during compilation it is asking for input?
Always use code tags instead of making the text bold.
Oct 4 '07 #4
r035198x
13,262 8TB
Thanks for quick reply.

I do not know how to set up the formula. When I run the file, it looks like this:

init:
deps-jar:
compile-single:
run-single:
Enter product number (1-3):



When I enter information, it becomes this:


init:
deps-jar:
compile-single:
run-single:
Enter product number (1-3):
1
Enter quantity ordered:
3
BUILD SUCCESSFUL (total time: 6 minutes 31 seconds)



Can you help how to figure out?
Many thanks.
The program does exactly what you told it to do.
What was your expected output?
Oct 4 '07 #5
JosAH
11,448 Expert 8TB
When I take a "bird's eye" view on that code I see:

Expand|Select|Wrap|Line Numbers
  1. int totalValue= 0;
  2.  
  3. // a whole lot of things that all have nothing to do with
  4. // 'totalValue', i.e. it isn't changed at all by any of those
  5. // complicated statements and finally we reach:
  6.  
  7. if (totalValue != 0) // sure, right
  8.    ...
  9.  
kind regards,

Jos
Oct 4 '07 #6
I do not know how to set a formula.

I want to buy, for example 2* Product1 + 5*Product2 + 3*Product3 and the print will show the total amount of all products.
Oct 4 '07 #7
I am a new member.
I got stuck using Switch in java. Could someone help me?

This is my code:
Thanks




Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class MailOrder { 
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.       Scanner input = new Scanner( System.in );//declare Scanner object
  8.  
  9.       int productID=0;//productID
  10.       double totalValue = 0; //total value of the order
  11.       double Product1 = 2.98;
  12.       double Product2 = 4.50;
  13.       double Product3 = 6.87;
  14.  
  15.       System.out.println("Enter product number (1-3): " );
  16.       productID = input.nextInt(); //read the product ID
  17.  
  18.       // determine the number sold of the item
  19.       System.out.println( "Enter quantity ordered: " );
  20.       int quantity = input.nextInt();
  21.  
  22.       //please complete the switch statement
  23.       switch (productID)
  24.       {
  25.           case 1:                     
  26.              quantity += quantity;
  27.              break;
  28.  
  29.           case 2:                       
  30.              quantity += quantity;
  31.              break;
  32.  
  33.           case 3:                     
  34.              quantity += quantity;
  35.              break;
  36.  
  37.          } // end switch
  38.  
  39.          if (totalValue !=0)
  40.              System.out.println ("Total Valude of this order is "+ totalValue);
  41.         }
  42.  
  43. }
I made it work. I changed line #26, 30, and 34.
However, I can enter only one product at a time. For example, the message asks: Enter product number (1-3), I can enter only 1 or 2 or 3.

How do I set up to NOT enter 4 or to enter 1 and 2 and 3 a tthe same time?


When I run the file, I got a message Enter product number (1-3). I entered 1 in the input box. However, the output looks like this:

Enter product number (1-3):
1

How do I move # 1 after the colon? For example: Enter product number (1-3): 1
Oct 4 '07 #8
r035198x
13,262 8TB
I made it work. I changed line #26, 30, and 34.
However, I can enter only one product at a time. For example, the message asks: Enter product number (1-3), I can enter only 1 or 2 or 3.

How do I set up to NOT enter 4 or to enter 1 and 2 and 3 a tthe same time?


When I run the file, I got a message Enter product number (1-3). I entered 1 in the input box. However, the output looks like this:

Enter product number (1-3):
1

How do I move # 1 after the colon? For example: Enter product number (1-3): 1
Just use an if else to make sure that the number supplied is either 1,2 or 3.
Oct 4 '07 #9
Ganon11
3,652 Expert 2GB
For your output message question, you are using System.out.println(). The println stands for print line, so it always moves to a new line once it is finished outputting the String inside. You should use System.out.print() instead. It does exactly the same stuff as println, but doesn't print a newline character at the end. This will result in your desired output.
Oct 4 '07 #10
For your output message question, you are using System.out.println(). The println stands for print line, so it always moves to a new line once it is finished outputting the String inside. You should use System.out.print() instead. It does exactly the same stuff as println, but doesn't print a newline character at the end. This will result in your desired output.
Thanks for your idea. I tried but still did not work as I expected.
Here is the code

System.out.println("\nEnter product number (1-3):");
productID = input.nextInt(); //read the product ID

// determine the number sold of the item
System.out.println( "Enter quantity ordered:" );
int quantity = input.nextInt();


And here is the output:

Enter product number (1-3):
2
Enter quantity ordered:
2
Total Valude of this order is $9.0
Oct 4 '07 #11
Ganon11
3,652 Expert 2GB
Thanks for your idea. I tried but still did not work as I expected.
Here is the code

Expand|Select|Wrap|Line Numbers
  1. System.out.println("\nEnter product number (1-3):");   
  2.       productID = input.nextInt(); //read the product ID
  3.  
  4.       // determine the number sold of the item
  5.       System.out.println( "Enter quantity ordered:" );
  6.       int quantity = input.nextInt();
OK, you're still using System.out.println().

Change this to System.out.print();
Oct 4 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: ron | last post by:
have been stuck on this for several days now. I am trying to create a reverse polish calculator and I'm stuck at an intermediate stage. This is what I know I have to do (just not sure how to do it...
4
by: Alistair | last post by:
I should just get a hat with a big D on it and then everyone would recognise me.. anyway... I have a DB full of people and their details.. I'm trying to perform a query based on age. so...
5
by: jamesc | last post by:
Putting together a database for network equipment. I want to do one simple thing and I can't figure it out. I want to make a form with a Multiselect List box that has a bunch of switches in it. I...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
2
by: azmiza | last post by:
Hi friend, I am new in this area. Hope members can help me on my script below. I want to create Previous and Next button but it runs until maximum execution time. Help me <?php #script...
2
by: rehet | last post by:
hye.. i really need ur guys help.. im stuck.. i want to count the word freq and print them according to the word length.. > the longest be output first.. ouh.. and my input must be alphabet or...
5
by: 1051109210 | last post by:
#include<iostream> using namespace std; int main(){ string seat; int row,i,j; char col,response,ok;
10
by: Cliff | last post by:
Greetings, I have been trying to teach myself C++ over the past few weeks and have finally came across a problem I could not fix. I made a simple program that prints out a square or rectangle...
1
by: Kburge03 | last post by:
Hi!! I've been working on this assingment for class where I have to design and implement an application that displays two Die objects, a button, and a label. Every time the button is pushed, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.