java - Printing out a while loop -
java - Printing out a while loop -
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 starting salary: "); salary = reader.nextdouble(); system.out.print("what precentage increase: "); pi = reader.nextdouble(); system.out.print("how many years working: "); year = reader.nextint(); if (salary <= 0){ system.out.print("the salary must positive."); } if (pi <= 0){ system.out.print("the percentage increment must positive."); } if (year < 0){ system.out.print("the years must positive."); } while (year > years) { predict = salary * (pi/100); system.out.println(years + ". " + predict); years++; if (years == year){ break; } } }
}
i having problem trying print out loop. every time run program, segment prints out 1 number , doesn't print out rest.
per @ajb wouldn't want in loop? print salary increment every year, , add together salary next iteration
per new comment
every time output, display year , salary.
while (year > years) { //predict = salary * (pi/100); commented because not necessary anymore. salary += salary * (pi/100); system.out.println(years + ". " + salary); // replaced predict salary, show salary , not predicted raise. years++; // block of code never hit, hence not needed. //if (years == year){ // break; //} }
revised this.
while (year > years) { salary += salary * (pi/100); system.out.println(years + ". " + salary); years++; }
java
Comments
Post a Comment