I have a problem that I raised before and as they told me, I need to show code and be more specific, so here I go. I have to introduce the variable precio
by Scanner in a method, and I want to later send that variable precio
to the main
to use it in the 2nd method. On the other hand, I would also like to use only one return
to round two figures, but I don't see how. My code:
import java.util.*;
public class Precios {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
preciosPorcentajes(teclado);
preciosSemanales(teclado, precio);
}
public static void preciosPorcentajes(Scanner teclado) {
System.out.print("Introduce precio: ");
double precio = teclado.nextDouble();
for (int i = 1; i <= 3; i++) {
double porcentaje = 1.0;
double intereses = precio * porcentaje / 100 * i;
System.out.print(i + " años \t" + intereses * (1.0) + "(1.0%) \t" + intereses * (1.5) + "(1.5%) \t");
}
}
public static void preciosSemanales(Scanner teclado, double precio) {
System.out.print("Introduce el Precio anual: ");
double precioAnual = teclado.nextDouble();
System.out.print("Introduce la tasa: ");
double tasas = teclado.nextDouble();
System.out.print("Introduce la duración: ");
int semanas = teclado.nextInt();
for(int i = 1; i <= semanas; i++) {
double intereses = precio * tasaInteres / 100;
intereses = redondear(precio, intereses);
System.out.println("Semana " + i);
System.out.println("\t Precio Anual: " + (precio));
System.out.println("\t Intereses: " + intereses);
precio += intereses + precioAnual;
precio = redondear(precio, intereses);
}
}
public static double redondear(double precio, double intereses) {
double num = Math.round(precio * 100) / 100.0;
double num = Math.round(intereses * 100) / 100.0;
return num;
}
}
Thanks for the help.
In that case, the method must not be declared as
void
, which means empty , or what is the same, it does not return anything. You must indicate that the method returns an object of type delreturn
.Something like that:
Now, in the call, you would do this:
But, it seems that
preciosPorcentajes()
interest must also be determined. In that case, several solutions are possible:precio
as theyinteres
are of the same type, you can declare an array to store both values and return them from the methodHashMap
ArrayList
precio
asinteres
members of the class if you plan to use them in multiple methods of the same class.Since I don't know the context, if it's an exercise, etc, I can't delve deeper into this, proposing the most optimal solution. In addition, the concept is not well understood
interes
in your context, since you use a loop, as if different interests should apply according to different contexts. As I said in a comment, a method is to do something and it is essential, when programming, to be clear about the purpose of that method, what it should do , and that should be clearly stated in the question.