denominator7 :
Problema con mi código para la calculadora - valores de salida no corrige Aquí está mi código, cualquier respuesta sería apreciada.
import java.util.Scanner;
public class Savings {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
//ask for initial amount
System.out.print("What is the initial savings amount? ");
double initialAmount = console.nextDouble();
// ask for number of months
System.out.print("What is the number of months to save? ");
int months = console.nextInt();
//ask for interest rate
System.out.print("What is the annual interest rate? ");
double interestRate = console.nextDouble();
//calculate total
double monthlyInterest = ((interestRate/100)*(1/12));
double number1 = (monthlyInterest + 1);
double number2 = Math.pow(number1, months);
double total = (initialAmount*number2);
System.out.println("$" + initialAmount + ", saved for " + months + " months at " + interestRate + "% will be valued at $" + total);
console.close();
}
}
valor final termina siendo el mismo valor que inicial
cardouken:
Cambia esto:
double monthlyInterest = ((interestRate/100)*(1/12));
a
double monthlyInterest = (interestRate / 100) * (1.0 / 12.0);
Usted está tratando de hacer la división entera en el contexto de punto flotante, por lo que en monthlyInterest
está multiplicando esencialmente interestRate / 100
con 0.