![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Web Design & Programming Discussion of web design, and server-side & client-side scripting |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Registered User
Join Date: Dec 2008
Posts: 6
OS: xp
|
Hi, I need help on an java assignment, here's what it needs to do:
Ask the user for weight value on Earth, and asked the user to choose planet and calculate their weight on that planet. 1. Mecury 2. Venus ~ for all planets in the solar system Reply: ex. 70 kg on Earth is 40kg on the moon. Ask for another weight Then let them put in another weight and loop. Quit the program when 0 is entered. Complain if invalid number is entered. Must use 'switch'(case) statements in code Here's the code I have so far: import java.util.*; public class Planetweight { public static void main(String[]args) { boolean done = false; //boolean = is program done or not int userChoice; //integer to hold user's choice //two doubles used in the equation double d1 , d2; //make multiple declarations with coma Scanner inputReader = new Scanner(System.in); System.out.print("Choose a planet to find out how much you weigh on that planet"); //give user choices System.out.println("Choose from the following options:\n"); System.out.println(" 1. Mercury"); //0.38 System.out.println(" 2. Venus"); //0.91 System.out.println(" 3. Earth"); //1.0 System.out.println(" 4. Mars"); //0.38 System.out.println(" 5. Jupiter"); //2.54 System.out.println(" 6 Saturn."); //107 System.out.println(" 7. Uranus"); //90 System.out.println(" 8. Neptune"); //1.15 System.out.println(" 9. Pluto"); //0.08 userChoice = inputReader.nextInt(); //ask the user for the two numbers System.out.print("\n Enter your current weight on Earth in kilograms:"); d1 = inputReader.nextDouble(); System.out.print(d1 + "kg on Earth is" ); } } the numbers after the // is the comparison of the gravitational pull on that planet compared to Earth's. any input would be appreciated
|
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
Moderator: Design
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)
|
Re: Need Help on Java Programming - Weight on other planets
Which part do you need help with? Are you just plain stuck, or do you have some idea where you need to go with this?
__________________
![]() Validate your Markup Validate your CSS Notepad++ Please use [html], [php], and [code] when posting code or markup. I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message. |
|
|
|
|
|
#3 (permalink) |
|
Registered User
Join Date: Dec 2008
Posts: 6
OS: xp
|
Re: Need Help on Java Programming - Weight on other planets
program is supposed to calculate the weight of the person on a different planet and respond in this format:
" 'x' kg on Earth is equal to 'y' kg on (selected planet)" I'm stuck on how to put the name of the planet selected by the user into the response.. |
|
|
|
|
|
#4 (permalink) |
|
Moderator: Design
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)
|
Re: Need Help on Java Programming - Weight on other planets
Looks like you're going to need a string for the planet name.
String planetname = null; You only need one switch to make this work. Read http://java.sun.com/docs/books/tutor...ts/switch.html for a very good explanation of how the switch statement works. I'll let you figure out the rest on your own. If you have any questions, please post back. This particular assignment has intrigued me, so I decided to build one as well :). The finicky validation and repeats stymied me, but I think I got it now.
__________________
![]() Validate your Markup Validate your CSS Notepad++ Please use [html], [php], and [code] when posting code or markup. I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message. |
|
|
|
|
|
#5 (permalink) |
|
Registered User
Join Date: Dec 2008
Posts: 6
OS: xp
|
Re: Need Help on Java Programming - Weight on other planets
Here's my current Code now:
import java.util.*; public class Planetweight { public static void main(String[]args) { boolean done = false; //boolean = is program done or not int userChoice; //integer to hold user's choice int i = 0; //two doubles used in the equation double d1; //declare d1 as a double Scanner inputReader = new Scanner(System.in); System.out.print("Choose a planet to find out how much you weigh on that planet.\n"); //give user choices System.out.println("Choose from the following options:\nType 0 to quit.\n"); System.out.println(" 1. Mercury"); //0.38 System.out.println(" 2. Venus"); //0.91 System.out.println(" 3. Earth"); //1.0 System.out.println(" 4. Mars"); //0.38 System.out.println(" 5. Jupiter"); //2.54 System.out.println(" 6 Saturn."); //1.07 System.out.println(" 7. Uranus"); //0.9 System.out.println(" 8. Neptune"); //1.15 System.out.println(" 9. Pluto"); //0.08 System.out.println(" 10. Moon"); //0.17 userChoice = inputReader.nextInt(); //ask the user for the two numbers System.out.print("\n Enter your current weight on Earth in kilograms:"); d1 = inputReader.nextDouble(); while (!done) { if (i==0) { System.out.println("Goodbye."); done = true; } else if (!(i==0)) switch(userChoice) { case 1: System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mercury."); break; //tells system to break out case 2: System.out.println(d1 + "kg on Earth is " + d1*0.91 + "kg on Venus."); break; case 3: System.out.println(d1 + "kg on Earth is " + d1*1.0 + "kg on Earth."); break; case 4: System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mars."); break; case 5: System.out.println(d1 + "kg on Earth is " + d1*2.54 + "kg on Jupiter."); break; case 6: System.out.println(d1 + "kg on Earth is " + d1*1.07 + "kg on Saturn."); break; case 7: System.out.println(d1 + "kg on Earth is " + d1*0.9 + "kg on Uranus."); break; case 8: System.out.println(d1 + "kg on Earth is " + d1*1.15 + "kg on Neptune."); break; case 9: System.out.println(d1 + "kg on Earth is " + d1*0.8 + "kg on Pluto."); break; case 10: System.out.println(d1 + "kg on Earth is " + d1*0.17 + "kg on the Moon."); break; default: //user picked a bogus value System.out.println("invalid input"); break; } } } } but I'm having problems getting the program to loop using WhileLoops btw, also needs to loop when user enters a invalid input |
|
|
|
|
|
#6 (permalink) |
|
Moderator: Design
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)
|
Re: Need Help on Java Programming - Weight on other planets
For validation, you are going to need several boolean variables and while statements. You could use do while statements, but they are not considered good practice. The while loop operates on a condition, just like any other loop statement. This condition can be as simple as x==1 or planet_Flag==false; more complex conditions can be created using operators, but this is unnecessary.
while statements are fairly simple to implement singly. Once you get into stacked loop statements, things become tricky. Code:
byte x == 1;
while (x==1)
{
As always, if you have any questions, post back :)
__________________
![]() Validate your Markup Validate your CSS Notepad++ Please use [html], [php], and [code] when posting code or markup. I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message. |
|
|
|
|
|
#7 (permalink) |
|
Registered User
Join Date: Dec 2008
Posts: 6
OS: xp
|
Re: Need Help on Java Programming - Weight on other planets
The code above is mainly composed of while loops, but I'm required by my teacher to use switchcases, so here's what I tried:
/* Ask the user for weight value on Earth, and asked the user to choose planet and calculate their weight on that planet. 1. Mecury 2. Venus ~ for all planets in the solar system Reply: ex. 70 kg on Earth is 40kg on the moon. Ask for another weight Then let them put in another weight and loop. Quit the program when 0 is entered. Complain if invalid number is entered. Must use 'switch'(case) statements in code Due: next Friday */ import java.util.*; public class Planetweight { public static void main(String[]args) { boolean done = false; //boolean = is program done or not int userChoice; //integer to hold user's choice //two doubles used in the equation double d1; //declare d1 as a double Scanner inputReader = new Scanner(System.in); System.out.print("Choose a planet to find out how much you weigh on that planet.\nType 0 to quit.\n \n"); //give user choices System.out.println(" 1. Mercury"); //0.38 System.out.println(" 2. Venus"); //0.91 System.out.println(" 3. Earth"); //1.0 System.out.println(" 4. Mars"); //0.38 System.out.println(" 5. Jupiter"); //2.54 System.out.println(" 6 Saturn."); //1.07 System.out.println(" 7. Uranus"); //0.9 System.out.println(" 8. Neptune"); //1.15 System.out.println(" 9. Pluto"); //0.08 System.out.println(" 10. Moon"); //0.17 userChoice = inputReader.nextInt(); while (userChoice = inputReader.nextInt()) //if the user did not type 0, the calculator will begin { //ask the user for weight System.out.print("\n Enter your current weight on Earth in kilograms:"); d1 = inputReader.nextDouble(); switch(userChoice) //takes the case chooses the action to perform { case 1: System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mercury."); break; //tells system to break out case 2: System.out.println(d1 + "kg on Earth is " + d1*0.91 + "kg on Venus."); break; case 3: System.out.println(d1 + "kg on Earth is " + d1*1.0 + "kg on Earth."); break; case 4: System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mars."); break; case 5: System.out.println(d1 + "kg on Earth is " + d1*2.54 + "kg on Jupiter."); break; case 6: System.out.println(d1 + "kg on Earth is " + d1*1.07 + "kg on Saturn."); break; case 7: System.out.println(d1 + "kg on Earth is " + d1*0.9 + "kg on Uranus."); break; case 8: System.out.println(d1 + "kg on Earth is " + d1*1.15 + "kg on Neptune."); break; case 9: System.out.println(d1 + "kg on Earth is " + d1*0.8 + "kg on Pluto."); break; case 10: System.out.println(d1 + "kg on Earth is " + d1*0.17 + "kg on the Moon."); break; case 0: System.out.println("Goodbye!"); System.exit(0); break; default: //user picked a bogus value System.out.println("Pick a valid number, retard"); break; } } if (userChoice==0) //tells the user program is done { System.out.println("Goodbye!!"); } } } but when I compile this, I get this error: ---------- Capture Output ---------- > "C:\Program Files\Java\jdk1.6.0_16\bin\javac.exe" Planetweight.java Planetweight.java:50: incompatible types found : int required: boolean while (userChoice = inputReader.nextInt()) //if the user did not type 0, the calculator will begin ^ 1 error > Terminated with exit code 1. but I don't know how to change it into boolean, cuz whatever I change will only return me with more errors.. |
|
|
|
|
|
#8 (permalink) |
|
Moderator: Design
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)
|
Re: Need Help on Java Programming - Weight on other planets
Code:
userChoice = inputReader.nextInt(); while (userChoice = inputReader.nextInt()) Operating conditions almost invariably consist of a variable, one or more operands, and a number or other variable(s). In this case, you want userChoice to be greater than or equal to zero. I would initialize userChoice to -1 at the start of each iteration to clear this condition. Code:
while (userChoice >= 0) Code:
if (userChoice==0) //tells the user program is done
{
System.out.println("Goodbye!!");
}
Good luck, and remember: sometimes the compiler does not have the best solution. PS: insulting the user will not win you any favours with the instructor. I learned this the hard way :S.
__________________
![]() Validate your Markup Validate your CSS Notepad++ Please use [html], [php], and [code] when posting code or markup. I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message. |
|
|
|
|
|
#10 (permalink) |
|
Moderator: Design
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)
|
Re: Need Help on Java Programming - Weight on other planets
Glad to hear it. Drop by any time if you're having trouble.
__________________
![]() Validate your Markup Validate your CSS Notepad++ Please use [html], [php], and [code] when posting code or markup. I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message. |
|
|
|
![]() |
| Thread Tools | |
|
|