java - 如何将 if/else if 代码中的整数转换为该代码外部的整数?(爪哇)
问题描述
我正在尝试编写一个可以解决指数问题的代码,并且可以解决整数输入和字输入(即一、二……等)的指数问题。我几乎弄清楚了整数部分,但是我被困在“单词输入”部分。主要是,我代码中单词部分的最后一部分(int base = y,exponent = z),我可以在其中计算指数不起作用,因为无法从 if/else if 语句中提取 y 和 z
我如何制作我的代码,以便 int base = y, exponent = z 可以从它们各自的 if/else if 代码中提取?
非常感谢!
public static void main(String[] args) {
Scanner choice = new Scanner(System.in);
System.out.println("Enter 1 for Alphabetical Input, 2 for numerical input:");
int solution = choice.nextInt();
if(solution == 1) {
for(int i=1; i<=100;i++) {
Scanner words = new Scanner(System.in);
System.out.print("Enter your base");
String x = words.toString();
String x1 = "zero";
String x2 = "one";
String x3 = "two";
String x4 = "three";
String x5 = "four";
String x6 = "five";
String x7 = "six";
String x8 = "seven";
String x9 = "eight";
String x10 = "nine";
String x11 = "ten";
if(x == x1) {
int y = 0;
}
else if(x == x2) {
int y = 1;
}
else if(x == x3) {
int y = 2;
}
else if(x == x4) {
int y = 3;
}
else if (x == x5) {
int y = 4;
}
else if (x == x6) {
int y = 5;
}
else if (x == x7) {
int y = 6;
}
else if (x == x8) {
int y = 7;
}
else if (x == x9) {
int y = 8;
}
else if (x == x10) {
int y = 9;
}
else if (x == x11) {
int y = 10;
}
else {
System.out.println("I can't read that!");
}
System.out.println("Enter your exponent");
Scanner exponent = new Scanner(System.in);
String a = exponent.toString();
if(a == x1) {
int z = 0;
}
else if(a == x2) {
int z = 1;
}
else if(a == x3) {
int z = 2;
}
else if(a == x4) {
int z = 3;
}
else if (a == x5) {
int z = 4;
}
else if (a == x6) {
int z = 5;
}
else if (a == x7) {
int z = 6;
}
else if (a == x8) {
int z = 7;
}
else if (a == x9) {
int z = 8;
}
else if (a == x10) {
int z = 9;
}
else if (a == x11) {
int z = 10;
}
else {
System.out.println("I can't read that!");
}
int base = y, exponents = z; //this does not work, y and z aren't pulled
double result = Math.pow(base, exponents);
}
}
else if(solution ==2 ) {
for(int i=1;i<=100;i++){
Scanner number = new Scanner(System.in);
System.out.print("Enter your base:");
int x = number.nextInt();
Scanner up = new Scanner(System.in);
System.out.print("Enter your exponent:");
int y = up.nextInt();
int base = x, exponent = y;
double result = Math.pow(base, exponent);
System.out.println("Answer = " + result);
}
}
else {
System.out.println("Could not read input");
}
}
}
解决方案
像@Shuang Li在 for 循环前面所说的那样声明它们,否则 y 和 z 仅在 about if 中可见。
除此之外,您不应该使用if (x == x1)
它来检查您的输入是否等于 String x1 的内存地址,因此将打印出“我无法读取!”。要检查字符串是否相等,您要使用if (x.equals(x1))
.
进一步阅读用户的输入String x = words.toString();
并没有做你希望它做的事情,因为它会立即跳到 if-part 传递null
给String x
. 相反,您应该使用
String x = words.next();
或者String x = words.nextLine();
与.toString();
方法不同.next();
,它们.nextLine();
是方法,Scanner
因此它们将等待任何输入,并且只有在获得输入后,代码才会被进一步执行。
所以这将是你的目标:
public static void main(String[] args) {
Scanner choice = new Scanner(System.in);
System.out.println("Enter 1 for Alphabetical Input, 2 for numerical input:");
int solution = choice.nextInt();
int y = 0;
int z = 0;
if (solution == 1) {
for (int i = 1; i <= 100; i++) {
Scanner words = new Scanner(System.in);
System.out.print("Enter your base");
String x = words.next();
String x1 = "zero";
String x2 = "one";
String x3 = "two";
String x4 = "three";
String x5 = "four";
String x6 = "five";
String x7 = "six";
String x8 = "seven";
String x9 = "eight";
String x10 = "nine";
String x11 = "ten";
if (x.equals(x1)) {
y = 0;
} else if (x.equals(x2)) {
y = 1;
} else if (x.equals(x3)) {
y = 2;
} else if (x.equals(x4)) {
y = 3;
} else if (x.equals(x5)) {
y = 4;
} else if (x.equals(x6)) {
y = 5;
} else if (x.equals(x7)) {
y = 6;
} else if (x.equals(x8)) {
y = 7;
} else if (x.equals(x9)) {
y = 8;
} else if (x.equals(x10)) {
y = 9;
} else if (x.equals(x11)) {
y = 10;
} else {
System.out.println("I can't read that!");
}
System.out.println("Enter your exponent");
Scanner exponent = new Scanner(System.in);
String a = exponent.next();
if (a.equals(x1)) {
z = 0;
} else if (a.equals(x2)) {
z = 1;
} else if (a.equals(x3)) {
z = 2;
} else if (a.equals(x4)) {
z = 3;
} else if (a.equals(x5)) {
z = 4;
} else if (a.equals(x6)) {
z = 5;
} else if (a.equals(x7)) {
z = 6;
} else if (a.equals(x8)) {
z = 7;
} else if (a.equals(x9)) {
z = 8;
} else if (a.equals(x10)) {
z = 9;
} else if (a.equals(x11)) {
z = 10;
} else {
System.out.println("I can't read that!");
}
int base = y, exponents = z; // this does not work, y and z aren't pulled
double result = Math.pow(base, exponents);
System.out.println("Answer = " + result);
}
} else if (solution == 2) {
for (int i = 1; i <= 100; i++) {
Scanner number = new Scanner(System.in);
System.out.print("Enter your base:");
int x = number.nextInt();
Scanner up = new Scanner(System.in);
System.out.print("Enter your exponent:");
y = up.nextInt();
int base = x, exponent = y;
double result = Math.pow(base, exponent);
System.out.println("Answer = " + result);
}
} else {
System.out.println("Could not read input");
}
}
推荐阅读
- python - 优化 softmax 函数
- android - Android 绑定库:找不到类型或命名空间
- python - 如何使用 Pandas 计算交叉点及其日期?
- python - 将半已知名称的文件导入pandas并返回文件名
- kotlin - 带有 TelegramApi javax.net.ssl.SSLHandshakeException 的机器人:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException
- flutter - 当我在 android 模拟器或 genymotion 上吃一个颤振应用程序时,我的笔记本电脑会关闭,但当我在同一个模拟器上吃一个 android 应用程序时不会
- python - 在 Django 中实现 Quantited-MultiSelect “材料列表”
- powerbi - Dax:在 Power BI 中找到高级经理列与经理姓名列比较的错误条目
- javascript - window.onload 函数永远不会执行
- apache-kafka - Kafka 流:如何在聚合时生成主题?