Java 如何使用嵌套循环分别输出整数及其展开形式的幂
我想输出功率表,从n(0)到n(10),仅使用扫描仪输入底座 现在,我在设置它时遇到了困难 代码: 预期产出:Java 如何使用嵌套循环分别输出整数及其展开形式的幂,java,loops,Java,Loops,我想输出功率表,从n(0)到n(10),仅使用扫描仪输入底座 现在,我在设置它时遇到了困难 代码: 预期产出: 5^0= 5^1= 5^2= 5^3= - - - several lines are skipped here - - - 5^10= 注意:这不是预期的输出,但我想自己尝试一下代码。这只是一个步骤,可以帮助我找到代码中的最终结果问题: 您在循环中使用了result,因此每次迭代都会重置它 您已将print语句放入循环中
5^0=
5^1=
5^2=
5^3=
- - - several lines are skipped here - - -
5^10=
注意:这不是预期的输出,但我想自己尝试一下代码。这只是一个步骤,可以帮助我找到代码中的最终结果问题:
result
,因此每次迭代都会重置它结果
与base
相乘,每次迭代未完成import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int option = keyboard.nextInt();
int base = keyboard.nextInt();
int exponent = keyboard.nextInt();
int result = 1;
if (option == 1) {
for (int x = base; x <= base; x++) {
if (exponent <= 1) {
System.out.print(base + "^" + exponent);
} else {
System.out.print(base + "^" + exponent + "=");
}
for (int y = 1; y <= exponent; y++) {
System.out.print(exponent == 1 ? "" : (base + (y < exponent ? "*" : "")));
result *= base;
}
System.out.print("=" + result);
}
}
}
}
[更新]
根据你的评论,你还没有学会,但我强烈建议你学会。以下给出的解决方案不使用三元运算符:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int option = keyboard.nextInt();
int base = keyboard.nextInt();
int exponent = keyboard.nextInt();
int result = 1;
if (option == 1) {
for (int x = base; x <= base; x++) {
if (exponent <= 1) {
System.out.print(base + "^" + exponent);
} else {
System.out.print(base + "^" + exponent + "=");
}
for (int y = 1; y <= exponent; y++) {
if (y < exponent) {
System.out.print(base + "*");
} else if (exponent != 1) {
System.out.print(base);
}
result *= base;
}
System.out.print("=" + result);
}
}
}
}
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
int option=keyboard.nextInt();
int base=keyboard.nextInt();
int exponent=keyboard.nextInt();
int结果=1;
如果(选项==1){
for(int x=base;x评论不用于扩展讨论;此对话已结束。你好,Arvind,你能看看这个问题吗?@HussainOmer-检查。你好,Arvind,你能回答这个问题吗:这不是GUI,而是arraysHussainOmer123的更多内容-我刚刚回答了一个与你编辑的问题类似的问题。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int option = keyboard.nextInt();
int base = keyboard.nextInt();
int exponent = keyboard.nextInt();
int result = 1;
if (option == 1) {
for (int x = base; x <= base; x++) {
if (exponent <= 1) {
System.out.print(base + "^" + exponent);
} else {
System.out.print(base + "^" + exponent + "=");
}
for (int y = 1; y <= exponent; y++) {
System.out.print(exponent == 1 ? "" : (base + (y < exponent ? "*" : "")));
result *= base;
}
System.out.print("=" + result);
}
}
}
}
1
2
5
2^5=2*2*2*2*2=32
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int option = keyboard.nextInt();
int base = keyboard.nextInt();
int exponent = keyboard.nextInt();
int result = 1;
if (option == 1) {
for (int x = base; x <= base; x++) {
if (exponent <= 1) {
System.out.print(base + "^" + exponent);
} else {
System.out.print(base + "^" + exponent + "=");
}
for (int y = 1; y <= exponent; y++) {
if (y < exponent) {
System.out.print(base + "*");
} else if (exponent != 1) {
System.out.print(base);
}
result *= base;
}
System.out.print("=" + result);
}
}
}
}