Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用循环查找生成参数的基的指数_Java_Loops_Exponent - Fatal编程技术网

Java 如何使用循环查找生成参数的基的指数

Java 如何使用循环查找生成参数的基的指数,java,loops,exponent,Java,Loops,Exponent,我试图使用循环来找到产生特定参数的给定基的指数。例如,在方程5^x=625中,5为基数,625为自变量。我知道在等式x=4中,但我不确定如何在我的回报中得到4 以下是我到目前为止的情况: public static int log(int base, int argument) { int result = 1; for (int i=0; i<=; i++) result = ; return result; } 公共静态int日志(int base,

我试图使用循环来找到产生特定参数的给定基的指数。例如,在方程5^x=625中,5为基数,625为自变量。我知道在等式x=4中,但我不确定如何在我的回报中得到4

以下是我到目前为止的情况:

public static int log(int base, int argument) {
    int result = 1;
    for (int i=0; i<=; i++) 
    result = ;
    return result;
}
公共静态int日志(int base,int参数){
int结果=1;

对于(inti=0;i这更像是一个数学问题

对于公式5^x=625,使用x=log查找x₅(625)=对数(625)/对数(5)

因此:

但也许您已经知道了这一点,因为您正确地命名了该方法。

类似于:

public static int log(int base, int argument) {
    if(argument <= 0 || base <= 0) {
        throw new IllegalArgumentException("This method only works with positive integers");
    }
    int result = 1;
    int i = 0;
    while(result < argument) {
       result = result * base;
       i++;
    }
    if(result == argument) {
       return i;
    } else {
        throw new IllegalArgumentException("There is no integer for x in base^x = argument");
    }
}
公共静态int日志(int base,int参数){

如果(argument计算在结果为1或更大时,您可以将参数除以基数的次数:

public static int log(int base, int argument) {
  int result;
  for (result=0; argument>=1 ; result++) {
    argument = argument/base;
  }
  return result;
}

您将如何手动执行此操作?考虑如何手动计算
2^10
,然后使用执行此计算的循环编写函数
exp()
。这将有助于您了解如何实现
log()
函数。你最好舍入而不是强制转换为int,因为强制转换会将2.9999999这样的情况截断为2,而不是正确的结果3。小心整数溢出。--易于测试。如果
结果<0
则发生溢出。你是对的,可能除法会更安全。实际上,我现在没有时间重写它,所以我让其他人来做。您所要做的就是将
result>0
添加到
while
循环条件中。然后,它将失败并按其应该的方式抛出异常。
public static int log(int base, int argument) {
    if(argument <= 0 || base <= 0) {
        throw new IllegalArgumentException("This method only works with positive integers");
    }
    int result = 1;
    int i = 0;
    while(result < argument) {
       result = result * base;
       i++;
    }
    if(result == argument) {
       return i;
    } else {
        throw new IllegalArgumentException("There is no integer for x in base^x = argument");
    }
}
public static int log(int base, int argument) {
  int result;
  for (result=0; argument>=1 ; result++) {
    argument = argument/base;
  }
  return result;
}