1. the program needs to convert btu to jules
2. Convert calories to joules
3. Convert joules to joules
4 exit the program.
if the user type anything other than 1-4 the program should print a error message.
here is my code. -
import java.util.Scanner;
-
-
public class joules {
-
public static void main (String [] args) {
-
final double BTUs = 1056;
-
final double calories = 4.184;
-
final double joule = 1;
-
Scanner stdin = new Scanner(System.in);
-
System.out.println ("1. Convert BTUs to joules");
-
System.out.println ("2. Convert Calories to joules");
-
System.out.println ("3. Convert joules to joules");
-
System.out.println ("4. Exit program");
-
int choice = stdin.nextInt();
-
while (choice != 4)
-
if (choice < 1 || choice > 3)
-
//converting to joules using constants
-
if (choice = 1) {
-
double amount = stdin.nextDouble();
-
double joules = amount * BTUs;
-
System.out.println (amount + " BTUs is " + joules "joules");
-
}
-
if (choice = 1) {double amount = stdin.nextDouble();
-
double joules = amount * calories;
-
System.out.println (amount + " Calories is " + joules + "joules");
-
}
-
if (choice = 1) {
-
double amount = stdin.nextDouble();
-
double joules = amount * joule;
-
System.out.println (amount + " joules is " + joules + "joules");
-
}
-
//statement for wrong entries
-
else {
-
System.out.println ("That is not a valid entry. Try again");
-
}
-
10 5720 horace1 1,510
Recognized Expert Top Contributor DeMan 1,806
Top Contributor
You do, of course realise that you only ever check for option 1 (and doesn't java require ==)
what is your problem?
I started writing the code to convert btu to joules. This code is not working for me
1. the program needs to convert btu to jules
2. Convert calories to joules
3. Convert joules to joules
4 exit the program.
if the user type anything other than 1-4 the program should print a error message.
here is my code. -
import java.util.Scanner;
-
-
public class joules {
-
public static void main (String [] args) {
-
final double BTUs = 1056;
-
final double calories = 4.184;
-
final double joule = 1;
-
Scanner stdin = new Scanner(System.in);
-
System.out.println ("1. Convert BTUs to joules");
-
System.out.println ("2. Convert Calories to joules");
-
System.out.println ("3. Convert joules to joules");
-
System.out.println ("4. Exit program");
-
int choice = stdin.nextInt();
-
while (choice != 4)
-
if (choice < 1 || choice > 3)
-
//converting to joules using constants
-
if (choice = 1) {
-
double amount = stdin.nextDouble();
-
double joules = amount * BTUs;
-
System.out.println (amount + " BTUs is " + joules "joules");
-
}
-
if (choice = 1) {double amount = stdin.nextDouble();
-
double joules = amount * calories;
-
System.out.println (amount + " Calories is " + joules + "joules");
-
}
-
if (choice = 1) {
-
double amount = stdin.nextDouble();
-
double joules = amount * joule;
-
System.out.println (amount + " joules is " + joules + "joules");
-
}
-
//statement for wrong entries
-
else {
-
System.out.println ("That is not a valid entry. Try again");
-
}
-
You should be using a switch for this. - int choice = stdin.nextInt();
-
switch(choice) {
-
case 1: {
-
-
break;
-
}
-
case 2: {
-
-
break;
-
}
-
.
-
.
-
.
-
default : {
-
//wrong input
-
-
}
-
What error did you get?
horace1 1,510
Recognized Expert Top Contributor
your logic in the following statement where you test if choice is between 1 and 3 inclusive is incorrect -
while (choice != 4)
-
if (choice < 1 || choice > 3)
-
you test if choice is less than 1 or greater than 3
you need to test if choice is greater than or equal to 1 AND less than or equal to 3, e.g. -
while (choice != 4)
-
if (choice >= 1 && choice <= 3)
-
also as DeMan indicated statements such as
should use ==
however, as indicated by r035198x, it would be better to use a switch statement
1. the program needs to convert btu to jules
2. Convert calories to joules
3. Convert joules to joules
4 exit the program.
if the user type anything other than 1-4 the program should print a error message.
I can not get this code to execute Please hellp
here is my code.
Code:
import java.util.*;
public class Project_2 {
// main(): application entry point
public static void main (String [] args) {
final double BTUs =1056;
final double calories =4.184;
final double joule =1;
int choice;
// set up input stream
Scanner stdin = new Scanner(System. in);
System.out.prin tln ("1. Convert BTUs to Joules");
System.out.prin tln ("2. Convert Calories to Joules");
System.out.prin tln ("3. Convert Joules to Joules");
System.out.prin tln ("4. Exit program");
int choice=stdin.ne xtInt();
//converting BTUS to Joules
switch (choice){
case 1:
double amount = stdin.nextDoubl e();
double joules = amount * BTUs;
System.out.prin tln (amount + "BTUs is " + joules + "joules");
break;
// converting Calories to Joules
case 2:
double joules = amount * calories;
System.out.prin tln (amount + "Calories is " + joules + "joules");
break;
// converting Joules to Joules
case 3:
double joules = amount * joule;
System.out.prin tln (amount + "joules is " + joules + "joules");
break;
// exit program
case 4:
System.exit(0)
break;
default:
System.out.prin tln ("You have entered an incorrect number, Please try again.");
}
}
}
1. the program needs to convert btu to jules
2. Convert calories to joules
3. Convert joules to joules
4 exit the program.
if the user type anything other than 1-4 the program should print a error message.
I can not get this code to execute Please hellp
here is my code.
Code:
import java.util.*;
public class Project_2 {
// main(): application entry point
public static void main (String [] args) {
final double BTUs =1056;
final double calories =4.184;
final double joule =1;
int choice;
// set up input stream
Scanner stdin = new Scanner(System. in);
System.out.prin tln ("1. Convert BTUs to Joules");
System.out.prin tln ("2. Convert Calories to Joules");
System.out.prin tln ("3. Convert Joules to Joules");
System.out.prin tln ("4. Exit program");
int choice=stdin.ne xtInt();
//converting BTUS to Joules
switch (choice){
case 1:
double amount = stdin.nextDoubl e();
double joules = amount * BTUs;
System.out.prin tln (amount + "BTUs is " + joules + "joules");
break;
// converting Calories to Joules
case 2:
double joules = amount * calories;
System.out.prin tln (amount + "Calories is " + joules + "joules");
break;
// converting Joules to Joules
case 3:
double joules = amount * joule;
System.out.prin tln (amount + "joules is " + joules + "joules");
break;
// exit program
case 4:
System.exit(0)
break;
default:
System.out.prin tln ("You have entered an incorrect number, Please try again.");
}
}
}
-
-
import java.util.*;
-
public class Project_2 {
-
// main(): application entry point
-
public static void main (String [] args) {
-
final double BTUs =1056;
-
final double calories =4.184;
-
final double joule =1;
-
// set up input stream
-
Scanner stdin = new Scanner(System.in);
-
System.out.println ("1. Convert BTUs to Joules");
-
System.out.println ("2. Convert Calories to Joules");
-
System.out.println ("3. Convert Joules to Joules");
-
System.out.println ("4. Exit program");
-
int choice=stdin.nextInt();
-
//converting BTUS to Joules
-
switch (choice){
-
case 1: {
-
System.out.println ("Enter the BTUs");
-
double amount = stdin.nextDouble();
-
double joules = amount * BTUs;
-
System.out.println (amount + "BTUs is " + joules + "joules");
-
break;
-
}
-
// converting Calories to Joules
-
case 2: {
-
System.out.println ("Enter the Calories");
-
double amount = stdin.nextDouble();
-
double joules = amount * calories;
-
System.out.println (amount + "Calories is " + joules + "joules");
-
break;
-
}
-
// converting Joules to Joules
-
case 3: {
-
System.out.println ("Enter the Joules");
-
double amount = stdin.nextDouble();
-
double joules = amount * joule;
-
System.out.println (amount + "joules is " + joules + "joules");
-
break;
-
}
-
// exit program
-
case 4: {
-
System.exit(0);
-
}
-
default: {
-
System.out.println ("You have entered an incorrect number, Please try again.");
-
}
-
}
-
}
-
}
-
-
You were missing declarations for amount in the other case statements. Now make it possible to make many conversions before the program exits. Currently you can do only one conversion and then the program exits. You will need to use a while loop.
Thanks a Lot this got me on the right track. I added a while (true){
to get the program to keep looping but I want to make it pause after each calulations. How do I do that.
Thanks
Minterman
Thanks a Lot this got me on the right track. I added a while (true){
to get the program to keep looping but I want to make it pause after each calulations. How do I do that.
Thanks
Minterman
How do you want it to pause? Pause until the user does something or pause for a specific period of time?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Steven T. Hatton |
last post by:
Is there anything that gives a good description of how source code is
converted into a translation unit, then object code, and then linked. I'm
particularly interested in understanding why putting normal functions in
header files results in multiple definition errors even when include guards
are used.
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla:...
|
by: Michael Lehn |
last post by:
Hi,
I have a question regarding the conversion of objects. When is the
conversion done by the constructor and when by the operator. My feeling
tells me that the constructor is preferred. But I couldn't find the
exact rule in the C++ standard.
And what if the classes have template parameters?
It would be great if somebody could get me a rough hint where in the
|
by: Bjørn Augestad |
last post by:
Below is a program which converts a double to an integer in two
different ways, giving me two different values for the int. The basic
expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable
becomes 364 and the other one becomes 365.
Does anyone have any insight to what the problem is?
Thanks in advance.
Bjørn
|
by: Martin |
last post by:
When I convert a program from VC6 to VC7 I get the
following linker error:
CVTRES : fatal error CVT1100: duplicate resource.
type:STRING, name:1686, language:0x0409
LINK : fatal error LNK1123: failure during conversion to
COFF: file invalid or corrupt
Duplicate resource...no problem! Except that I cannot
find that value anywhere!! No such string resource
|
by: Javaman59 |
last post by:
I just saw an interesting identifier in a C# book I'm reading...
double dollarsPerHead
This jumped out at me, as it is not common to put units in an identifer. We
don't see "millisecondsSinceStart" ("time"), or "milesPerHour" ("speed").
But, perhaps it's a good idea. If memory serves me right, one of the Mars
landers crashed because two development teams used different units (metric
and imperial), across an interface. Also, when I'm...
| |
by: TheSeeker |
last post by:
Hi,
As part of a larger project, I am trying to use the GNU Units program
to provide unit conversions between quantities.
My first iteration, which worked OK, was to simply use units as a
command-line app, and capture its output. The larger program however,
calls the conversion routine many times, and the overhead of starting
units for every call made things too slow.
I then re-formulated my use of units to open it using popen2, and do
|
by: dhruba.bandopadhyay |
last post by:
I am trying to port an old Pascal DOS game to DOS C/C++. I am wondering
if anyone is familar with the dos & crt Pascal units and whether there
are C/C++ equivalent libraries. Maybe dos.c & crt.c?
Below lists names of variables, functions, types & weird interrupt
procedures found in Pascal. Am wondering what can be done to get around
them for use in C/C++.
dos.pas
crt.pas
|
by: beatdream |
last post by:
I am designing a database to handle different kinds of products ... and
these products can have different properties...for example, a trouser
can be specified by the width, length, color, and other additional
properties...while you might need only one property for another
product, e.g litres for a soft drink...so I was thinking of creating a
properties table, a units table and a product table, properties-unit
table...
and...
|
by: kwikius |
last post by:
Well lads :-)... looking at your Boost Units library the impression I
get is that you got your library into boost Before writing anything
much apart from some documentation, which was AFAICS even noted and
allowed for in the review managers preamble. Nice work boost ... :-)
Why the library was moved to the top of the review queue and accepted
in such an unfinished state has I conjecture a lot to do with...
hmm ... the BoostCon circus?
...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |