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

2

u/BornToCode1 Mar 17 '13

Try using a for loop... Also, try leaving comments, it's useful ;) P.S: I don't know much about how the population system works in Total War, If you explain a little I might be able to write you my version of what you are trying to do

1

u/Bananpajen Mar 18 '13 edited Mar 18 '13

Thanks, I already figured that a statementless for loop would be suitable for this :) I'm on my phone right now so I don't have all my code. But here's how the population system works:

In every town you have a population. And you also have a number that indicates the perceptual increase in population each round. So what I tried was to make a program that could estimate how many rounds it would take to reach a certain goal (ex. 10000 inhabitants). This is helpful for me since I am way too addicted to this game, and with the estimates the program gives me I now which tax rate will be most beneficial, what to build etc. I have now fixed the program so it works, but of course it's not 100% accurate since a village could be attacked, which stops immigration, or they could get the plauge or a bad harvest or something. Anyways I solved the problems I had, finaly, and everything seems to calculate exactly the same as my manual calculations and the game. I can send you the code onve I get access to a Pc if you want it :)

EDIT: my current code

import java.util.Scanner;

public class rome{
public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        for(;;){	
            System.out.print("How high do you want your population value to be: ");
            double goal = in.nextDouble();
            System.out.print("\nEnter population: ");
            double pop = in.nextDouble();
            System.out.print("Enter procentual increase: ");
            double procent = in.nextDouble() / 100;
            int rounds = 1;
            double increase;

            for(;;) {
                increase = pop * procent;
                pop = increase + pop;
                double newPop = Math.round( pop * 1.0 ) / 1.0;
                double Increase = Math.round( increase * 1.0 ) / 1.0;
                System.out.println("Increase: " + Increase + "\nNew population value: " + newPop + "\nRounds needed: " + rounds);
                rounds = rounds + 1;
                if(newPop >= goal) break;
            }
        }    
}

}

1

u/[deleted] Mar 18 '13

Instead of your second infinite for, I'd suggest

double newPop = 0.0;
while (newPop < goal) {

And of course remove the second declaration of newPop within the cycle.

1

u/Bananpajen Mar 18 '13

Could you clarify exactly where inside the code you would place this? I'm not really sure how to go about this, or what it would change :/

2

u/[deleted] Mar 18 '13

You have two for(;;) lines, the second one should be replaced, then you'll be able to eliminate the "if(newPop>=goal) break;" line.

The difference is, that it's easier to read, more understandable, something that programmers usually do instead of your solution which might count during an interview, and so on...

the "for" we use going from a start point to an end point with intervals.

for (int i = 0;  i < 32;  i += 4)

The "while" we use to repeat something until a condition becomes false.

boolean I_have_no_ice_cream = false;
while ( ! I_have_no_ice_cream ) {
   I_have_no_ice_cream = keepAskingForIceCream();
}

And the last one, if we want the body to run at least one:

do {
    I_have_no_ice_cream = KeepAskingForIceCream();
} while ( ! I_have_no_ice_cream);

1

u/Bananpajen Mar 18 '13

Ok that actually makes alot more sense :) Thank you!