zu bauen Ich versuche, einen grundlegenden Rechner für die Klasse zu machen.Ich versuche, einen grundlegenden Rechner mit if Anweisungen
Ich kam mit dem Code unten, aber alles es tut es zusätzlich.
Auch wenn ich -
, *
oder /
verwende.
Irgendwelche Tipps?
Scanner input = new Scanner(System.in);
String num1, num2, operation, function;
System.out.println("Enter a simple equation: ");
function = input.nextLine();
int firstspace = function.indexOf(" ");
num1 = (function.substring(0,firstspace));
int lastspace = function.lastIndexOf(" ");
operation = (function.substring(firstspace,lastspace));
num2= (function.substring(lastspace+1,function.length()));
double n1 = Double.parseDouble(num1);
double n2 = Double.parseDouble(num2);
if (operation.equals(" + "));
{
System.out.println("your answer is " + (n1 + n2));
}
if (operation.equals(" - "))
{
System.out.println("your answer is " + (n1 - n2));
}
if (operation.equals("/"))
{
System.out.println("your answer is " + (n1/n2));
}
if (operation.equals(" * "))
{
System.out.println("your answer is " + (n1*n2));
}
}
}
Welcome hilft SO :-) Sie Ihren Code debuggen versucht haben? –