Java 如何使用循环计算指数?
我目前正在开发一个计算某些数字幂的程序。数字限制为1到9。我的代码贴在下面。我有以下问题:Java 如何使用循环计算指数?,java,Java,我目前正在开发一个计算某些数字幂的程序。数字限制为1到9。我的代码贴在下面。我有以下问题: 每次我运行程序时,它都不会打印正确的答案 我想修改代码,以便应用程序计算X到Y的幂,其中X和Y可以是1到9(包括9)范围内的整数。如果用户输入无效值,程序应再次要求用户输入。当用户输入完基值和指数后,程序将打印结果 这个任务的条件是,我必须使用循环来计算结果 多次乘法;我不允许使用任何可用的方法或API 这就为我计算出了结果。请帮我想出解决办法 /* * To change this template,
/*
* 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
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装指数;
//导入java.util.Scanner;
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int b,e;
System.out.println(“输入基数”);
b=Integer.parseInt(br.readLine());
System.out.println(“输入电源”);
e=Integer.parseInt(br.readLine());
int t=1;
for(int i=1;i对于一个开始,for循环后面不应该有分号:
for(int i=1;i<=e; i++ )
{
t=t*b;
}
首先,For循环后面不应该有分号:
for(int i=1;i<=e; i++ )
{
t=t*b;
}
从for循环中删除分号
从
for(int i=1;i从for循环中删除分号
从
for(inti=1;i对于第一部分,它是一个简单的修复方法。您只是在for循环中不应该有分号的地方添加了一个分号
for(int i = 1;i <= e; i++); {
for(int i = 1;i <= e; i++){ //There should be no semicolon here
如果代码片段令人困惑,下面是整个可编译的工作类:
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.
}
}
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
公共类StackOverflowAnswers{
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int b,e;
是否{//询问基地
System.out.println(“输入基数”);
b=Integer.parseInt(br.readLine());
}while(b>9 | | b<1);//如果基无效,它将返回到“do”语句,该语句再次请求基。
你要电源吗
System.out.println(“输入电源”);
e=Integer.parseInt(br.readLine());
}while(e>9 | | e<1);//如果电源无效,它将返回到“do”语句,该语句再次请求电源。
int t=1;
for(inti=1;i对于第一部分,这是一个简单的修复方法。您只是在for循环中不应该有分号的地方添加了一个分号
for(int i = 1;i <= e; i++); {
for(int i = 1;i <= e; i++){ //There should be no semicolon here
如果代码片段令人困惑,下面是整个可编译的工作类:
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.
}
}
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
公共类StackOverflowAnswers{
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int b,e;
是否{//询问基地
System.out.println(“输入基数”);
b=Integer.parseInt(br.readLine());
}while(b>9 | | b<1);//如果基无效,它将返回到“do”语句,该语句再次请求基。
你要电源吗
System.out.println(“输入电源”);
e=Integer.parseInt(br.readLine());
}while(e>9 | | e<1);//如果电源无效,它将返回到“do”语句,该语句再次请求电源。
int t=1;
对于(int i=1;i对于第一部分,之所以会发生这种情况,是因为您在循环的声明后放置了一个分号,而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;
}
import java.io.*;
公共课abc{
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int b,e;
System.out.println(“输入基数”);
b=检查(Integer.parseInt(br.readLine());
System.out.println(“输入电源”);
e=检查(Integer.parseInt(br.readLine());
int t=1;
对于(int i=1;i 10)
x=Integer.parseInt(br.readLine());
返回x;
}
对于第一部分,之所以会发生这种情况,是因为您在循环的声明后放置了一个分号,而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;
}
import java.io.*;
公共课abc{
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int b,e;
System.out.println(“输入基数”);
b=检查(Integer.parseInt(br.readLine());
System.out.println(“输入电源”);
e=检查(Integer.parseInt(br.readLine());
int t=1;
对于(int i=1;i 10)
x=Integer.parseInt(br.readLine());
返回x;
}
事实上这可能就是问题所在。我只是想告诉你,重复的答案没有问题。该网站并不总是实时更新。不过,另一个人删除了他的答案真是太好了。@ZongZhengLi I只是告诉另一个回答者答案已经被删除了posted@imulsion,两个职位之间的差距为l不到一分钟,虽然帖子现在被删除了。@我决定删除我的帖子,不是因为我不得不这样做,而是因为我认为不需要我的重复帖子。作为记录,+1用于识别问题并在我面前发布。实际上,这可能是
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.
}
}
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;
}