如何用循环计算指数?

时间:2013-11-14 17:46:39

标签: java

我目前正在开发一个计算某些数字能力的程序。数量限制是1到9.我的代码发布在下面。我有以下问题:

  1. 每次运行程序时,都不会打印正确的答案。

  2. 我想修改代码,以便应用程序计算X到Y的幂,其中X和Y允许为1到9(包括9)范围内的整数。如果用户输入无效值,程序应该再次询问用户输入。当用户输入base和exponents的值时,程序将打印结果。

  3. 这项任务的条件是我必须使用循环来计算结果 几次乘法;我不允许使用任何可用的方法或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
    }
    

4 个答案:

答案 0 :(得分:6)

首先,for循环后应该没有半冒号:

for(int i=1;i<=e; i++ )
        {
            t=t*b;
        }

简单的输入测试可能是:

public boolean testInput(int e)
{
if(e>9||e<1)//where e is the inputted number
{
return false
}
else 
{
return true;
}

}

然后像这样使用它:

  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;
  }
  }

答案 1 :(得分:0)

从for-loop中删除半冒号

来自

 for(int i=1;i<=e; i++ );

 for(int i=1;i<=e; i++ )

答案 2 :(得分:0)

对于第一部分,它是一个简单的解决方案。你刚刚在for循环中添加了一个分号。

for(int i = 1;i <= e; i++); {

for(int i = 1;i <= e; i++){ //There should be no semicolon here

对于第二部分,您可以使用两个非常简单的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);

所以do-while循环首先要求基数或幂(取决于程序运行的代码中的位置),然后将int设置为值。如果该值大于9,即:10或更高,则程序将重新为基数或幂(如我所说,依赖于哪个loo正在运行),然后它将再次设置int。它会这样做,直到值低于10.你想要的。

以下是输出示例:

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

如果代码片段令人困惑,这里是整个可编辑的工作阶段:

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.
    } 
}

希望这有帮助。

答案 3 :(得分:0)

对于第一部分,它刚刚发生,因为你在循环声明后放置了一个分号,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;
}