HEY GUYS! I really need help with my code! I recently started taking Java and I
ID: 3871581 • Letter: H
Question
HEY GUYS! I really need help with my code! I recently started taking Java and I hit a brick wall, I'm not understanding what's wrong with my code so if anyone can lend some guidance I would be enternally greatful! Thank you guys!
Suppose that the tuition for a university is $10,000 this year
and tuition increases 0.07 every year. After 11 years, the tuition
will be doubled.
Please write a program to
- ask the user the initial tuition and yearly
increase rate
- calculate how many years the tuition will be doubled.
MY CODE:
import java.util.Scanner;
public class TuitionSolver {
*/
public static void main(String[] args) {
double initTuition = 10000;
double increaseRate = 0.07;
Scanner input = new Scanner(System.in);
System.out.println("Please type in the inital tuition: ");
initTuition = input.nextFloat();
System.out.println("Plase type the increae rate: ");
increaseRate = input.nextFloat();
double finalTuition=initTuition;
int years = 0;
while(initTuition<20000){
finalTuition = initTuition * 1.07;
years++;
}
System.out.println("After " + years + " years, the tuition will be doubled.");
}
}
Explanation / Answer
CODE:
import java.util.Scanner;
public class dhahs
{
public static void main(String[] args) {
double initTuition = 10000; // u can assign directly or just define and leave it
double increaseRate = 0.07;
Scanner input = new Scanner(System.in);
System.out.println("Please type in the inital tuition: ");
initTuition = input.nextFloat(); // it overwrites the values or assign values
System.out.println("Plase type the increae rate: ");
increaseRate = input.nextFloat();
double finalTuition=initTuition;
int years = 0;
while(finalTuition < 2*initTuition)
{
finalTuition =finalTuition+ finalTuition * increaseRate; // int the loop ur increasing the final value
years++;
}
System.out.println("After " + years + " years, the tuition will be doubled.");
}
}
output:
Please type in the inital tuition:
10000
Plase type the increae rate:
0.07
After 11 years, the tuition will be doubled.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.