打印出一个循环

时间:2014-11-12 22:00:30

标签: java

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;
        }
    }    
}

}

我在尝试打印循环时遇到问题。每次运行程序时,此段仅打印出一个数字,不会打印出其余的数字。

1 个答案:

答案 0 :(得分:0)

每个@ajb你不想在你的循环中做这样的事情吗?这将打印您每年的工资增长,并将其添加到下一次迭代的工资

根据您的新评论

  

每次他们是输出,我希望它显示年份和   他们的工资。

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;
    //}
}   

修订可能是这样的。

while (year > years) {
    salary += salary * (pi/100);
    System.out.println(years + ". " + salary);
    years++;
}