我似乎无法理解此代码的问题。我是Java新手。我在这里创建了一个新的Java类。我已经尝试了几乎所有的东西,但是我的构造函数下面的行上的错误现在真的很烦人。有人可以帮我解决这里有什么问题吗?
P.s主类完全为空,粗体的行是编译器显示错误的行。
import java.util.Scanner;
public class calculator extends FinalConsole {
float x;
float y;
float v;
float w;
float z;
char op;
Scanner in= new Scanner(System. in);
public calculator(){
System.out.println("You have selected calculator interface");
}
**System.out.println("Enter the number of digits: ");
int values;
values=in.nextInt();
System.out.println("Select your funcion");
op = in.next().charAt(0);**
**if (values==2){**
System.out.println("Enter first number: ");
x= in.nextFloat( );
System.out.println("Enter second number: ");
y=in.nextFloat();
switch (op){
case '+':
z=x+y;
System.out.println("sum is:" +z);
break;
case '-':
z = x-y;
System.out.println("sum is:" +z);
break;
case '/':
z=y/x;
System.out.println("sum is:" +z);
break;
case '*':
z=x*y;
System.out.println("sum is:" +z);
break;
default:
System.out.println("Invalid operator");
}
}
**if (values==3){**
System.out.println("Enter first number: ");
x= in.nextFloat( );
System.out.println("Enter second number: ");
y=in.nextFloat();
System.out.println("Enter third number: ");
v= in.nextFloat( );
switch(op){
case '+':
z=w+x+y;
System.out.println("sum is:" +z);
break;
case '-':
z=x-y;
System.out.println("sum is:" +z);
break;
case '/':
z=y/x;
System.out.println("sum is:" +z);
break;
case '*':
z=w*x*y;
System.out.println("sum is:" +z);
break;
default:
System.out.println("Invalid operator");
break;
}
}
**if (values==4){**
System.out.println("Enter first number: ");
x= in.nextFloat( );
System.out.println("Enter second number: ");
y=in.nextFloat();
System.out.println("Enter third number: ");
v= in.nextFloat( );
System.out.println("Enter fourth number: ");
w= in.nextFloat( );
switch (op){
case '+':
z=v+w+x+y;
System.out.println("sum is:" +z);
break;
case '-':
z=x-y;
System.out.println("sum is:" +z);
break;
case '/':
z=y/x;
System.out.println("sum is:" +z);
break;
case '*':
z=v*w*x*y;
System.out.println("sum is:" +z);
break;
default:
System.out.println("Invalid operator");
break;
}
}
}
答案 0 :(得分:0)
你应该把你的陈述放在块或方法中,如下面的
import java.util.Scanner;
public class calculator {
float x;
float y;
float v;
float w;
float z;
char op;
Scanner in= new Scanner(System. in);
public calculator(){
System.out.println("You have selected calculator interface");
}
{
System.out.println("Enter the number of digits: ");
int values;
values = in.nextInt();
System.out.println("Select your funcion");
op = in.next().charAt(0);
}
}