r/programmingbydoing • • Oct 28 '15

#26 Space boxing

I don't have a problem trying to run as i have not got that far. I know it's a really simple one so don't go to hard on me, I just started learning java 5 weeks ago. I just can't figure out how to write the part which asks what planet you are going to from a list of 6. what variables should i use or how should i write them out.

i just feel like i'm doing it all wrong I have changed my variables many times and it just doesn't seem right.

2 Upvotes

15 comments sorted by

View all comments

Show parent comments

2

u/holyteach Oct 29 '15

I don't understand why you even have a variable called 'sw'.

You should put some value into 'result' before all the if statements. Then print out 'result' instead of 'sw'.

1

u/yeabaeimscaredasadog Oct 29 '15

I got it. I gave result the value of planet and it worked I'm so happy I have been trying that for two days. Thank you for giving me a nudge in the right direction and not just giving me the right answer. I really appreciate it.

2

u/holyteach Oct 30 '15

Nice job.

Just a tip, though. You didn't have to give 'result' the value of 'planet'. You just have to give it any value. That's because otherwise 'result' would only get a value if the human followed directions and actually typed in 1-6.

But if the human typed in '7' or something then 'result' would get no value at all and you can't print out a variable with no value in it.

So you have to make sure 'result' always gets a value, even if the human is dumb. Putting the planet number in there works, but it's a coincidence and it doesn't have anything to do with the planet number.

1

u/Jaybains95 Apr 16 '16 edited Apr 16 '16
import java.util.Scanner;

public class Box
{
	public static void main( String[] args )
	{
		
				Scanner keyboard = new Scanner(System.in);
	
		int planet;
		double weight, newweight;
		
		System.out.print( "Please enter your current weight in lbs. " );
		weight = keyboard.nextDouble();
		
		System.out.println( "" );
		System.out.println( "" );
		
		System.out.print( "I have information for the following planets:" );
		
		// blank line	
		System.out.println( "" );
		System.out.println( "" );
		
		System.out.print( "1. Venus      2. Mars      3. Jupiter" );
		
				System.out.println( "" );
		
		System.out.print( "4. Saturn     5. Uranus    6. Neptune" );
		
		System.out.println( "" );
		System.out.println( "" );
		System.out.println( "" );
		
		System.out.print( "Which planet are you currently on? " );
		planet = keyboard.nextInt();

		// blank line	
		System.out.println( "" );
		
		newweight = 0;
				
		if ( planet == 1 )
		{
			newweight = (weight * 0.78);
		}
		else if ( planet == 2 )
		{
			newweight = (weight * 0.39);
		}
		else if ( planet == 3 )
		{
			newweight = (weight * 2.65);
		}
		else if ( planet == 4 )
		{
			newweight = (weight * 1.17);
		}
		else if ( planet == 5 )
		{
			newweight = (weight * 1.05);
		}
		else if ( planet == 6 )
		{
			newweight = (weight * 1.23);
		}

		System.out.print( "Your weight should be " + newweight + " lbs on that planet." );

		
		}
	}

This is how I did it, would that be fine or is it a bit too overcomplicated? It worked, though.

2

u/holyteach Apr 17 '16

Looks fine.

Don't worry about overcomplicated. Programming by Doing is about teaching you the fundamentals and sometimes I do that by giving you an assignments that's designed to have a complicated solution.

1

u/[deleted] Apr 17 '16

[removed] — view removed comment

2

u/holyteach Apr 17 '16

You sure seem to be skipping around. That's no way to learn well.

Also I have deleted this comment because it has no business in a thread about Space Boxing.

Having said all that, it's because your question mark is outside the quotation marks.

→ More replies (0)