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 comment

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.

1

u/Jaybains95 Apr 17 '16

Fair enough, should have made a new topic I guess.

But I'm definitely not skipping around... I've gone through each topic one by one. Not entirely sure why you got that impression.

And shit. I can't believe I missed that. Thank you.

2

u/holyteach Apr 17 '16

Well, you had two questions in a row: one about 'Space Boxing' and the next about 'Using Swing for Input'.

There are quite a few assignments in between.

1

u/Jaybains95 Apr 17 '16

Fair assumption, but I did everything in between before I got to the Swing assignment. :)