This site requires JavaScript, please enable it in your browser!
Greenfoot back
coder04
coder04 wrote ...

2015/9/7

Inputting and calculating a result

1
2
coder04 coder04

2015/9/7

#
I'm trying to make a program with a function of inputting a number then it outputs the results and adds them together. For example: 1 car needs 2 wheels and 4 doors 1 truck needs 6 wheels and 2 doors the user can enter the amount of trucks and cars they want separately and then the program will tell you how much wheels and doors you need altogether. Thanks for your help :)
Super_Hippo Super_Hippo

2015/9/7

#
(I guess a car needs 4 wheels:)
int cars = Greenfoot.ask("How many cars?");
int trucks = Greenfoot.ask("How many trucks?");
System.out.println("You need " + (cars*4 + trucks*6) + " wheels and " + (cars*4 + trucks*2) + " doors.");
coder04 coder04

2015/9/7

#
Haha sorry thats what I meant but would this be private int and would it go in a world subclass
danpost danpost

2015/9/7

#
The 'ask' method in the Greenfoot class returns a String value, not an 'int'; so, the code will need adjusted for that. The code can be placed in the world constructor provided the 'ask' method is executable there. Otherwise, it can go either in a 'public void started' method in your world class or in the 'act' method where it would need to be regulated by a conditional expression. It could also be within a 'do' loop in the 'started' method where the input can be checked for zero values to cause termination.
coder04 coder04

2015/9/7

#
When I put it in the world class in public void act it highlights
("How many cars?");
and says incompatable types
danpost danpost

2015/9/7

#
coder04 wrote...
When I put it in the world class in public void act it highlights
("How many cars?");
and says incompatable types
That is precisely what was I was indicating with this.
danpost wrote...
The 'ask' method in the Greenfoot class returns a String value, not an 'int'; so, the code will need adjusted for that.
You cannot assign a String value to a variable declared to hold an 'int' value. You can first assign the returned value to a String variable an then convert it, if possible, to an 'int' value and assign that to an 'int' variable.
Super_Hippo Super_Hippo

2015/9/8

#
That happens if I suggest methods I never used...
int cars = Integer.parseInt(Greenfoot.ask("How many cars?"));
coder04 coder04

2015/9/21

#
Thanks for the help! I would like to add something as well. I was wondering what if you have to buy the doors and wheels in packs. For example: you can only buy 5 doors, 10 doors, 15 doors not separately so if u need 8 doors you would need to buy the 10 door pack
danpost danpost

2015/9/21

#
Add one less than the number that come in a pack to how many are needed and then divide by the number that come in a pack. That will give you how many packs need to be bought (as 'int' values, the division will round down).
coder04 coder04

2015/9/22

#
Would I need to add that bit to this or a separate one
System.out.println("You need " + (cars*4 + trucks*6) + " wheels and " + (cars*4 + trucks*2) + " doors.")
danpost danpost

2015/9/22

#
coder04 wrote...
Would I need to add that bit to this or a separate one
System.out.println("You need " + (cars*4 + trucks*6) + " wheels and " + (cars*4 + trucks*2) + " doors.")
Yeah -- you can do it there. Just make sure the entire expression that represents the end value is enclosed within round brackets (as they are in the given line).
coder04 coder04

2015/10/14

#
I tried what you said but I think I must be doing it wrong can you help me do it because it will not compile
danpost danpost

2015/10/14

#
coder04 wrote...
I tried what you said but I think I must be doing it wrong can you help me do it because it will not compile
Cannot correct unseen code. Post what code you are having trouble with.
coder04 coder04

2015/11/2

#
System.out.println("You need " + ((cars*4-1/10) + (trucks*6-1/10)) + " wheels and " + ((cars*4)-1/4 + (trucks*2-1/4)) + " doors.")
danpost danpost

2015/11/3

#
coder04 wrote...
System.out.println("You need " + ((cars*4-1/10) + (trucks*6-1/10)) + " wheels and " + ((cars*4)-1/4 + (trucks*2-1/4)) + " doors.")
The only thing I see wrong with the line that could make it not compile is that is does not end with a semi-colon. The expressions for the total number of packs of wheels and doors to buy are flawed. The following shows the operations needed to determine the number of pack (for wheels, in this case) that are needed:
int wheelsPerPack = 5;
int wheelPacksNeeded = (cars*4+trucks*6+(wheelsPerPack-1))/wheelsPerPack;
Adding one less than the number of items in the pack increments the end result when a partial pack is needed to complete the order.
There are more replies on the next page.
1
2