Java 如何用循环计算指数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19984703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate exponent with loop?
提问by
I am currently working on a program that calculates the power of certain numbers. The number limit is 1 to 9. My code is posted below. I have the following issues:
我目前正在开发一个计算某些数字的幂的程序。数量限制是 1 到 9。我的代码贴在下面。我有以下问题:
Every time I run the program it doesn't print the correct answer.
I want to modify the code so the application calculates X to power of Y, where X and Y are allowed to be integers in the range 1 to 9 (including 9). If the user enters an invalid value the program should ask the user for input again. When a user is done with entering the values for base and exponents, the program will print the result.
每次我运行程序时,它都不会打印正确的答案。
我想修改代码,以便应用程序计算 X 到 Y 的幂,其中 X 和 Y 可以是 1 到 9(包括 9)范围内的整数。如果用户输入了无效值,程序应再次要求用户输入。当用户输入完基数和指数的值后,程序将打印结果。
Conditions of this task is that I must use loops to calculate the result by doing several multiplications; I am not allowed to use any available method or API that calculates the result for me. Please help me come up with the solution.
这个任务的条件是我必须使用循环来通过多次乘法来计算结果;我不得使用任何可用的方法或 API 来为我计算结果。请帮我想出解决办法。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package exponent;
//import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int b,e;
System.out.println("Enter the base");
b = Integer.parseInt(br.readLine());
System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());
int t = 1;
for(int i = 1;i <= e; i++);
{
t=t*b;
}
System.out.println(t);
}
// TODO code application logic here
}
回答by imulsion
For a start, there should be no semi colon after the for loop:
首先,for 循环后不应该有分号:
for(int i=1;i<=e; i++ )
{
t=t*b;
}
A simple input test could be something along the lines of:
一个简单的输入测试可能是这样的:
public boolean testInput(int e)
{
if(e>9||e<1)//where e is the inputted number
{
return false
}
else
{
return true;
}
}
Then use it like this:
然后像这样使用它:
boolean valid = false;
while(valid!=true)
{
e = Integer.parseInt(br.readLine());
if(testInput(e)==false)
{
System.out.println("Please enter a number between 1 and 9")
continue;
}
else
{
valid = true;
}
}
回答by MouseLearnJava
Remove semi colon from for-loop
从 for 循环中删除分号
From
从
for(int i=1;i<=e; i++ );
to
到
for(int i=1;i<=e; i++ )
回答by Matthew S.
For the first part, its is a easy fix. You just added a semicolon where there shouldn't be in the for loop.
对于第一部分,它很容易修复。您刚刚在 for 循环中不应该出现的地方添加了一个分号。
for(int i = 1;i <= e; i++); {
for(int i = 1;i <= e; i++){ //There should be no semicolon here
For the second part, you can do it with two very easy do-while loops.
对于第二部分,您可以使用两个非常简单的 do-while 循环来完成。
//Replace this
System.out.println("Enter the base");
b = Integer.parseInt(br.readLine());
System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());
//with
do{
System.out.println("Enter the base");
b = Integer.parseInt(br.readLine());
}while(b > 9 || b < 1);
do{
System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());
}while(e > 9 || e < 1);
So the do-while loops, will first ask for the base or the power (Depending where in the code the program is running), then it will set the int to the value. If the value is greater than 9, ie: 10 or above, the program will reask for the base or power (Like I said, depended which loo is running), and then it will set the int again. It will do this, until the value is under 10. Like you want.
所以 do-while 循环将首先询问基数或功率(取决于程序在代码中运行的位置),然后将 int 设置为该值。如果该值大于 9,即:10 或更高,程序将重新请求 base 或 power(就像我说的,取决于哪个 loo 正在运行),然后它会再次设置 int。它会这样做,直到值低于 10。就像你想要的那样。
Here is an example of the output:
下面是一个输出示例:
Enter the base
56
Enter the base
-4
Enter the base
4
Enter the power
67
Enter the power
10
Enter the power
-8
Enter the power
7
4 to the 7th power is 16384
If the code snippets are confusing, here is the entire compilable, working class:
如果代码片段令人困惑,这里是整个可编译的工作类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StackOverflowAnswers{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int b,e;
do{ //Asks for the base
System.out.println("Enter the base");
b = Integer.parseInt(br.readLine());
}while(b > 9 || b < 1); //If the base is not valid, it goes back to the "do" statement, which asks for the base, again.
do{ //Asks for the power
System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());
}while(e > 9 || e < 1); //If the power is not valid, it goes back to the "do" statement, which asks for the power, again.
int t = 1;
for(int i = 1;i <= e; i++){ //No semicolon here
t=t*b;
}
System.out.println(b + " to the " + e + "th power is " + t); //Just added some words and the base and the power for easier debugging and understanding.
}
}
Hope this helps.
希望这可以帮助。
回答by user2984602
For the first part, it is just happening because you placed a semicolon after loop's declaration, which java just loops to that semicolon and nothing more. By removing semicolon the loop should work. However, for the second part, you can just add inputcheck method, as shown in my code below.
对于第一部分,它只是发生,因为您在循环声明之后放置了一个分号,而 java 只是循环到该分号,仅此而已。通过删除分号,循环应该可以工作。但是,对于第二部分,您可以添加 inputcheck 方法,如下面的代码所示。
import java.io.*;
public class abc {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int b, e;
System.out.println("Enter the base");
b = check(Integer.parseInt(br.readLine()));
System.out.println("Enter the power");
e = check(Integer.parseInt(br.readLine()));
int t = 1;
for (int i = 1; i <= e; i++); {
t = t * b;
}
System.out.println(t);
}
private static int check(int x) {
while (x < 1 || x > 10)
x = Integer.parseInt(br.readLine());
return x;
}