我正在构建一个计算器,但如果我没有“罪”左边的任何东西,我就得不到输出。但是,如果我把数字放在“罪”之前,那么它将执行操作,并将数字的错误告诉我。我希望它能够给你一个数字的罪,即使把它作为......“sin43”
import java.util.Scanner;
public class InputScanner_operations {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str;
System.out.println("Input your equation");
str = input.nextLine();
int X = str.indexOf("X");
int x = str.indexOf("x");
int div = str.indexOf("/");
int plu = str.indexOf("+");
int sub = str.indexOf("-");
int sin = str.indexOf("s");
int type = 0;
if (X != -1)
type = 1;
else if (x != -1)
type = 2;
else if (div != -1)
type = 3;
else if (plu != -1)
type = 4;
else if (sub != -1)
type = 5;
else if (sin != -1)
type = 6;
else
type = 0;
switch (type) {
case 1: {
int pos = str.indexOf("X");
for (int i = 1; i <= pos; i++) {
String upToNCharacters = str.substring(0,
Math.min(str.length(), i));
String downToNCharacters = str.substring(pos + 1,
Math.max(str.length(), i));
if (i == pos) {
Double left = Double.parseDouble(upToNCharacters);
Double right = Double.parseDouble(downToNCharacters);
System.out.println(left * right);
}
}
}
break;
case 2: {
int pos = str.indexOf("x");
for (int i = 1; i <= pos; i++) {
String upToNCharacters = str.substring(0,
Math.min(str.length(), i));
String downToNCharacters = str.substring(pos + 1,
Math.max(str.length(), i));
if (i == pos) {
Double left = Double.parseDouble(upToNCharacters);
Double right = Double.parseDouble(downToNCharacters);
System.out.println(left * right);
}
}
}
break;
case 3: {
int pos = str.indexOf("/");
for (int i = 1; i <= pos; i++) {
String upToNCharacters = str.substring(0,
Math.min(str.length(), i));
String downToNCharacters = str.substring(pos + 1,
Math.max(str.length(), i));
if (i == pos) {
Double left = Double.parseDouble(upToNCharacters);
Double right = Double.parseDouble(downToNCharacters);
System.out.println(left / right);
}
}
}
break;
case 4: {
int pos = str.indexOf("+");
for (int i = 1; i <= pos; i++) {
String upToNCharacters = str.substring(0,
Math.min(str.length(), i));
String downToNCharacters = str.substring(pos + 1,
Math.max(str.length(), i));
if (i == pos) {
Double left = Double.parseDouble(upToNCharacters);
Double right = Double.parseDouble(downToNCharacters);
System.out.println(left + right);
}
}
}
break;
case 5: {
int pos = str.indexOf("-");
for (int i = 1; i <= pos; i++) {
String upToNCharacters = str.substring(0,
Math.min(str.length(), i));
String downToNCharacters = str.substring(pos + 1,
Math.max(str.length(), i));
if (i == pos) {
Double left = Double.parseDouble(upToNCharacters);
Double right = Double.parseDouble(downToNCharacters);
System.out.println(left - right);
}
}
}
case 6: {
int pos = str.indexOf("s");
for (int i = 1; i <= pos; i++) {
String upToNCharacters = str.substring(0,
Math.min(str.length(), i));
String downToNCharacters = str.substring(pos + 3,
Math.max(str.length(), i));
if (i == pos) {
@SuppressWarnings("unused")
Double left = Double.parseDouble(upToNCharacters);
Double right = Double.parseDouble(downToNCharacters);
System.out.println(Math.sin(right));
}
}
}
break;
default:
System.out.println("Please check your formatting");
}
}
}
答案 0 :(得分:0)
在这段代码中:
case 6: {
int pos = str.indexOf("s");
for (int i = 1; i <= pos; i++) {
您找到"s"
的索引,然后开始for
循环。但是如果sin
位于开头,则pos
将为0而不是1,因此您的for
循环将永远不会执行。请记住,String
的编号为0。
尝试在int i = 0
循环中将其更改为int i = 1
而不是for
。