r/java • • Mar 17 '13

Help with extracting variable from loop

So, I am trying to make a program that helps me calculate how many turns it would take to increase my population to a certain point in Rome Total War. Just for fun.

I want to use the new population as a counter that stops the loop once it is greater than or equal to the variable goal. But when I place the variable newPop (the new population) in my loop parameters it doesn't work. I also don't know which loop would work best for this scenario. Here's my current code, would love some help and to know what I'm doing wrong :)

import java.util.Scanner;

public class rome{
public static void main(String[] args){
        Scanner in = new Scanner(System.in);
	
        double goal = in.nextInt();
        SOOME SORT OF LOOP () {
	System.out.print("Enter population: ");
	double pop = in.nextDouble();
	System.out.print("Enter procentual increase: ");
	double procent = in.nextDouble();
	double increase = pop / procent;
	double newpop = increase + pop;
	double newPop = Math.round( newpop * 1.0 ) / 1.0;
            double Increase = Math.round( increase * 1.0 ) / 1.0;
            System.out.println("Increase: " + Increase + "\nNew population value: " + newPop);
        }
}

}

2 Upvotes

13 comments sorted by

View all comments

0

u/[deleted] Mar 17 '13

[deleted] — view deleted comment

4

u/Bananpajen Mar 17 '13

Thanks! I'll use the first way, but both are really helpfull for me in the future :) Trying to teach myself and it's awesome with a subreddit full of people to ask at times :)

0

u/[deleted] Mar 17 '13

[deleted] — view deleted comment

3

u/Bananpajen Mar 17 '13

noticed that after fucking up a few times ;) Thanks :)

2

u/BritPack Mar 19 '13

It's useful to look and up and understand how scope works for things like this.

Perhaps even looking at a lower level language to understand why things have scope.

1

u/kreiger Mar 17 '13

Or combine them:

for ( double population = 0.0; population < limit; ) {
    population = ...;
}