【发布时间】:2018-01-05 09:57:52
【问题描述】:
当代码运行并输入输入时,应该解决方程的输出只打印先前输入的方程。请注意,代码中的注释在很大程度上是不正确的,因为它们来自代码的早期迭代。
public static void main(String[] args){
// introduces the user to the programme
System.out.println("Welcome to my calculator test");
System.out.println("Function inputs; + to add numbers, - to subtract numbers, * to multiply, / to devide, ^ to power,# to square root.");
//creates a Scanner
Scanner reader0 = new Scanner(System.in);
//promts the user for an input
System.out.println("Please enter first number");
String a = reader0.nextLine();
reader0.close();
char test = 'x';
String test1 = "";
for (int i = 0; i < a.length (); i++ ){
test = a.charAt(i);
if (Character.isDigit(test) || test == ('.')){
System.out.println(test);
test1 = test1+""+test;
}
else if (test == '+' ||test == '-' ||test == '*' ||test == '/' ||test == '#' ||test == '^'){
char b = a.charAt(i);
}
}
System.out.println(test);
System.out.println(test1);
double number = Double.parseDouble(test1);
// checks that if the user intends to squareroot the first number
if (test == '#'){
// square roots the first number
System.out.println(java.lang.Math.sqrt(a));
}
else{
//creates a Scanner
Scanner reader2 = new Scanner(System.in);
//promts the user for an input
System.out.println("Please enter Second number");
String c = reader2.nextLine();
reader2.close();
// checks if user wanted to add
if (test == '+'){
// adds the two numbers
System.out.println(a+c);
}
//checks if user wanted to subtract
else if (test == '-'){
// subtracts the two numbers
//System.out.println(a-c);
}
//checks if user wanted to multiply
else if (test == '*'){
// multiplies the two numbers
//System.out.println(a*c);
}
//checks if the user wanted to divide
else if (test == '/'){
// devides the two numbers
//System.out.println(a/c);
}
//checks if the user wants to power the numbers
else if (test == '^'){
// powers the first number by the second number
//System.out.println(java.lang.Math.pow(a,c));
}
// if the programme is unsure what the user intended then a error message will be printed to screen
else{
System.out.println("ERROR.UserInputForFunctionVoid");
}
}
}
对于解决此问题的任何帮助将不胜感激。
谢谢。
【问题讨论】:
-
Please note that the notes within the code are largely incorrect as they are from a earlier iteration of the code- 请努力从您发布的代码中删除不相关的 cmets。 -
System.out.println(a+c);a 和 c 是字符串,因此+运算符不执行数字加法。 -
"只打印之前输入的方程式" - 这不应该编译。
标签: java debugging calculator