public class New
{
public static void main(String[] args)
{
System.out.println("Calculator");
float a = StdIn.readFloat();
char sign = StdIn.readChar();
float b = StdIn.readFloat();
float c = 0;
if (sign == '+') c = a+b;
else if (sign == '-') c = a-b;
else if (sign == 'x') c = a*b;
else if (sign == '/') c = a/b;
System.out.println(c);
}
}
我需要一些代码帮助,我试图制作一个带有字符和浮点数据类型的StdIn输入的计算器。
答案 0 :(得分:1)
尝试Scanner
课程。
像这样:
import java.util.Scanner;
public class New
{
public static void main(String[] args)
{
System.out.println("Calculator");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Parameter ");
System.out.print("a : ");
float a = scanner.nextFloat();
System.out.print("+|-|*|/: ");
String op = scanner.next();
System.out.print("b : ");
float b = scanner.nextFloat();
float c = 0;
switch (op)
{
case "+":
c = a + b;
break;
case "-":
c = a - b;
break;
case "*":
c = a * b;
break;
case "/":
c = a / b;
break;
default:
System.out.println("Illegal operant");
}
System.out.println("Result: " + c);
}
}
我添加了一个switch case而不是else ifs。