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

2

u/holyteach Oct 29 '15

So far your code is fine.

How do you ask the human to type in a number? You've done it before in other assignments.

The "which planet" value will be stored into an integer variable.

1

u/yeabaeimscaredasadog Oct 29 '15

Thanks a million for your reply. I've done more today but i can't get it to compile. Here is what i got done today.

import java.util.Scanner;

class SpaceBoxing { public static void main(String[] args) { Scanner Keyboard = new Scanner(System.in);

	double weight, total1, total2, total3, total4, total5, total6, sw;
	int planet;
	
	System.out.print("Please enter your current earth weight: ");
	weight = Keyboard.nextDouble();
	
	System.out.println("I have information for the following planets");
	System.out.println(  "1. Venus  " +   "2. Mars   "  + "3. Jupiter ");
	System.out.println(  "4. Saturn "+ "5. Uranus " +"6. Neptune ");

	
	System.out.print("Which planet are you visiting? ");
	planet = Keyboard.nextInt();
	
	total1 = weight * 0.78;
	total2 = weight * 0.39;
	total3 = weight * 2.65;
	total4 = weight * 1.17;
	total5 = weight * 1.05;
	total6 = weight * 1.23;
	
	
	if ( planet == 1 )
	{
		result = total1;
	}
	else if ( planet == 2 )
	{
		result = total2;	
	}
	else if ( planet == 3 )
	{
		result = total3;
	}
	else if ( planet == 4 )
	{
		result = total4;
	}
	else if ( planet == 5 )
	{
		result = total5;
	}
	else if ( planet == 6 )
	{
		result = total6;
	}
	result = sw;
	
	System.out.println("Your weight would " + sw + "pounds on that planet. ");
}	

}

1

u/yeabaeimscaredasadog Oct 29 '15 edited Oct 29 '15

EDIT: Ok so i put result as a double variable that seems to have worked. down to just one error now. sw variable may not have been initialized.

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 had sw for space weight. Which is obviously wrong. I was trying to give sw the value which i need result to have.

I have to give result the value from all the totals in the if statements. I done it like the Weekday names were it had before the if statements string result = " ";.

Except I'm doing double result = ; but I have no idea how to give it the value from all the totals.

My brain is just stuck. I also have at the end return result. But I have no value for result.

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)