Please help! I'm confused what value the variable result gets to multiply base by. By the way, I'm a beginner at java. I have to write a program for exponents that raises a base to a power WITHOUT USING Math.pow. Also, I'm having trouble with my if statement because it doesn't work. (When I remove the if statement everything works, but I don't understand how it works...) For the if statement, I want it so if the user types in 0 as the power then it displays the base.
public static int exponent(int result) // defining method for raising base to power
{
int base; // defiing user input variables
int power;
{
Scanner reader = new Scanner(System.in); // scanner is called reader
System.out.println("Please enter the base."); // first input is base
base = reader.nextInt();
System.out.println("Please enter the exponent."); // second input
power = reader.nextInt();
for (int i = 1; i <= power; i++) {
result = base * result;
System.out.println("Result: " + base + " ^ " + power + " = " + result);
return 0;
{
if (power == 0)
System.out.println("Result: " + base);
}
return power;
}
Aucun commentaire:
Enregistrer un commentaire