-1
我正在建立一個計算器,但如果我沒有任何東西在左邊的「罪惡」,我沒有得到一個輸出。但是如果我把數字放在「罪」之前,它會執行操作並給我右邊的數字的罪。我希望它能夠給你即使把儘可能許多的罪......「sin43」IndexOf和SIN
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");
}
}
}
深思熟慮:如果你進入'罪惡45 +罪惡120'會發生什麼? – JonK 2014-10-30 16:43:07
這是我們篩選的很多代碼。你認爲問題在哪裏?我們不想浪費時間試圖理解與問題無關的代碼。 – 2014-10-30 16:43:55
雖然我有時質疑'indexOf'的道德問題,我不會稱之爲「SIN」。 – 2014-10-30 17:02:37