I am a new member.
I got stuck using Switch in java. Could someone help me?
This is my code:
Thanks - import java.util.Scanner;
-
-
public class MailOrder {
-
-
public static void main(String[] args) {
-
-
Scanner input = new Scanner( System.in );//declare Scanner object
-
-
int productID=0;//productID
-
double totalValue = 0; //total value of the order
-
double Product1 = 2.98;
-
double Product2 = 4.50;
-
double Product3 = 6.87;
-
-
System.out.println("Enter 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();
-
-
//please complete the switch statement
-
switch (productID)
-
{
-
case 1:
-
quantity += quantity;
-
break;
-
-
case 2:
-
quantity += quantity;
-
break;
-
-
case 3:
-
quantity += quantity;
-
break;
-
-
} // end switch
-
-
if (totalValue !=0)
-
System.out.println ("Total Valude of this order is "+ totalValue);
-
}
-
-
}
11 1535
What are you having trouble with?
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.
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.
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?
When I take a "bird's eye" view on that code I see: -
int totalValue= 0;
-
-
// a whole lot of things that all have nothing to do with
-
// 'totalValue', i.e. it isn't changed at all by any of those
-
// complicated statements and finally we reach:
-
-
if (totalValue != 0) // sure, right
-
...
-
kind regards,
Jos
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.
I am a new member.
I got stuck using Switch in java. Could someone help me?
This is my code:
Thanks - import java.util.Scanner;
-
-
public class MailOrder {
-
-
public static void main(String[] args) {
-
-
Scanner input = new Scanner( System.in );//declare Scanner object
-
-
int productID=0;//productID
-
double totalValue = 0; //total value of the order
-
double Product1 = 2.98;
-
double Product2 = 4.50;
-
double Product3 = 6.87;
-
-
System.out.println("Enter 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();
-
-
//please complete the switch statement
-
switch (productID)
-
{
-
case 1:
-
quantity += quantity;
-
break;
-
-
case 2:
-
quantity += quantity;
-
break;
-
-
case 3:
-
quantity += quantity;
-
break;
-
-
} // end switch
-
-
if (totalValue !=0)
-
System.out.println ("Total Valude of this order is "+ totalValue);
-
}
-
-
}
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
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.
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.
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
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();
OK, you're still using System.out.println().
Change this to System.out.print();
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: 1051109210 |
last post by:
#include<iostream>
using namespace std;
int main(){
string seat;
int row,i,j;
char col,response,ok;
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |