-2
import java.util.Scanner;

public class Teacher{

public static void main(String [] args){

    Scanner reader = new Scanner(System.in);

    double salary;
    double pi;
    int year;
    int years = 1;
    double predict;
    double predict2 = 0;
    double sum = 0;

    System.out.print("What is your starting salary: ");
    salary = reader.nextDouble();
    System.out.print("What is your precentage increase: ");
    pi = reader.nextDouble();
    System.out.print("How many years are you working: ");
    year = reader.nextInt();

    if (salary <= 0){
        System.out.print("The salary must be positive.");
    }
    if (pi <= 0){
        System.out.print("The percentage increase must be positive.");
    }
    if (year < 0){
        System.out.print("The years must be positive.");
    }   

    while (year > years) {

        predict = salary * (pi/100);

        System.out.println(years + ". " + predict);
        years++;

        if (years == year){
            break;
        }
    }    
}

}

I am having trouble trying to print out a loop. Every time I run the program, this segment only prints out one number and doesn't print out the rest.

Haedrian
  • 4,240
  • 2
  • 32
  • 53
  • 4
    If I say `X + Y != 30`, how can you help me if you don't know what X and Y are? Consider adding some context. – Sotirios Delimanolis Nov 12 '14 at 22:01
  • i added in the whole main method – Bjorn Lustic Nov 12 '14 at 22:03
  • Is this happening regardless of what "year" you enter? – NSimon Nov 12 '14 at 22:07
  • 1
    I think I know what you're asking, but you didn't state it well. So the problem is that it prints out several lines of output but the predicted salary is the same every time? (That sounds like the correct answer at many companies :( ) You really should say that, and include the input and output along with why you think the output is wrong. – ajb Nov 12 '14 at 22:07
  • Yes exactly, sorry this is my first time using this. – Bjorn Lustic Nov 12 '14 at 22:08
  • But you wouldn't leave out this much information when asking a friend, would you? We at Stack Overflow are no better at reading minds than anyone else. – ajb Nov 12 '14 at 22:10
  • Anyway, the reason your output is the same every time is because you calculate it by using the same numbers every time. Can you think of a way to change this? – ajb Nov 12 '14 at 22:10
  • What's the expected output? Is years changing among the different prints? Is predict supposed to include years omehow in the calculation part? Because this is supposed to print something like (let's assume predict is 10) 1.10 2.10 3.10 until year == years – NSimon Nov 12 '14 at 22:11
  • I am trying to write a program that allows for a user (teacher) to input their starting salary, percentage increase, and years that they have been working for as a teacher. I would like to display their salary over time. Every time their is an output, I would like it to display the year and their salary. I am having trouble repeating the equation and printing out every year, as I get the same salary every time a year prints out. – Bjorn Lustic Nov 12 '14 at 22:14
  • @BjornLustic it's because you don't keep track of the last salaray (or percentage gain per year worked). You should probably have a look at what austin wernli did – NSimon Nov 12 '14 at 22:15

1 Answers1

0

per @ajb wouldn't you want to do something like this in your loop? This will print your salary increase every year, and also add that to the salary for the next iteration

Per your new comment

Every time their is an output, I would like it to display the year and their salary.

while (year > years) {

    //predict = salary * (pi/100); commented this because it is not necessary anymore.
    salary += salary * (pi/100);

    System.out.println(years + ". " + salary); // replaced predict with salary, to show their salary and not just their predicted raise.
    years++;

    // This block of code will never be hit, therefore it is not needed.   
    //if (years == year){
    //    break;
    //}
}   

Revised could be something like this.

while (year > years) {
    salary += salary * (pi/100);
    System.out.println(years + ". " + salary);
    years++;
}   
austin wernli
  • 1,801
  • 12
  • 15
  • Actually you don't even need the last if statement :) – NSimon Nov 12 '14 at 22:12
  • The conditionals in this statement are very unclear to what he means... The first conditional states don't read the last year, while the last conditional means he should have printed the last year. Therefore i left it there because i wasn't sure heh. – austin wernli Nov 12 '14 at 22:13
  • While using a double, how do I only output the salary to two decimal places? – Bjorn Lustic Nov 12 '14 at 22:21