Cassie Robar
Cassie Robar

Reputation: 11

Yearly price increase loop? Stuck on nested loop

I'm trying to write a program that uses scanner keyboard to input these values and uses a loop to have them increase (2006-2010, year-year, & an increase in price, 90-100, etc.)

The output is supposed to look like this:

Enter the current Mars bar price in cents: 90
Enter the expected yearly price increase: 15
Enter the start year: 2006
Enter the end year: 2010
Price in 2006: 90 cents
Price in 2007: 105 cents
Price in 2008: 120 cents
Price in 2009: 135 cents
Price in 2010: 150 cents

This is the code I have so far:

import java.util.Scanner;
public class Problem_2 {

    public static void main(String[] args) {

        // TODO, add your application code
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the current Mars bar price in cents: ");
        int m = keyboard.nextInt();
        System.out.print("Enter the expected yearly price increase: ");
        int p = keyboard.nextInt();
        int a = m+p;

        int count = 1;
        System.out.print("Enter the start year: ");
        int start = keyboard.nextInt();
        System.out.print("Enter the end year: ");
        int end = keyboard.nextInt();

        for (int i = start; i <= end; i++){
            System.out.print("Price in " +i+ ": " +m);start++;
    }
}

and it allows me to have the year increasing to the set amount (again like 2006-2010) but I'm stuck on having the price increase along with it by the set amount, I assume it would be a nested loop but I'm not sure how to write it.

(I assume a nested loop because that's what we're learning right now.)

If anyone has any advice on what I could write, I'd really appreciate it!

Upvotes: 0

Views: 138

Answers (1)

LS_
LS_

Reputation: 7129

I think you just need to sum the expected yearly price increase to the current price in each iteration of your loop, it'd be something like this:

import java.util.Scanner; 
public class Problem_2 {

  public static void main(String[] args) {

    // TODO, add your application code
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter the current Mars bar price in cents: ");
    int m = keyboard.nextInt();
    System.out.print("Enter the expected yearly price increase: ");
    int p = keyboard.nextInt();

    int count = 1;
    System.out.print("Enter the start year: ");
    int start = keyboard.nextInt();
    System.out.print("Enter the end year: ");
    int end = keyboard.nextInt();

    for (int i = start; i <= end; i++){
        System.out.print("Price in " +i+ ": " +m);
        m += p; // sum expected price for each iteration

  }
}

Upvotes: 0

Related Questions