试图在java上创建一个简单的计算器。没有错误出现在代码中。但它仍然不起作用。我错过了什么?Calculator int choice = Integer.parseInt(char_a); java
import java.util.Scanner;
public class JavaApplication15 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("This is a calculator. Enter a letter followed by a number to calculate it.");
System.out.println(" S = sine \n C = Cosine \n T = Tangent \n R = Square Root \n N = natural Log \n X = exit the program");
String num = in.nextLine();
String sValue = num.substring(2);
String char_a = num.substring(0);
int choice = Integer.parseInt(char_a);
double dValue = Double.parseDouble(sValue);
while (choice != 'x'){
switch(choice){
case 's':
Math.sin(dValue);
System.out.println("The sine of your number is " + dValue);
break;
case'c':
Math.cos(dValue);
System.out.println("The Cosine of your number is " + dValue);
break;
case't':
Math.tan(dValue);
System.out.println("The Tangent of your number is " + dValue);
break;
case'r':
Math.sqrt(dValue);
System.out.println("The square root of your number is " + dValue);
break;
case'n':
Math.log(dValue);
System.out.println("The Log of your number is " + dValue);
break;
case'x':
break;
}
}
}
}
请扩大“不起作用”,因为这并不能告诉我们很多事情可以帮助我们理解您的问题。 –
有可能是一些错误。但这是一个明显的例子:在Math.sin(dValue)语句中,您计算值的正弦值,然后将结果丢弃。如果你想使用它,你必须把结果赋值给某个东西。 – ajb