import java.util.Scanner;
public class At18_1 {
public static void main(String[] args) {
System.out.println("请输入a的值:");
System.out.println("请输入b的值:");
Scanner a=new Scanner(System.in);
Scanner b=new Scanner(System.in);
double c=a*a+b*b;//这里报错 未对参数类型java.util.Scanner定义运算符"*"
System.out.println(c);
}
}
------解决方案--------------------
import java.util.Scanner;楼主你的Scanner用法不对,先通过new Scanner(System.in);创建的是对象a和b,然后再通过new出来的对象调用其中的方法来获取用户输入值。
public class At18_1 {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
Scanner b=new Scanner(System.in);
int num1;
int num2;
System.out.println("请输入num1的值:");
num1=a.nextInt();
System.out.println("请输入num2的值:");
num2=b.nextInt();
double c=num1*num1+num2*num2;
System.out.println(c);
}
}
------解决方案--------------------
楼主疏忽了吧,楼主是用Scanner来相乘啦,补充楼上的没有关闭资源,希望楼主以后养成一个好的编码习惯,I/O流一定要记得关闭:
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
Scanner b = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
System.out.println("请输入num1的值:");
num1 = a.nextInt();
System.out.println("请输入num2的值:");
num2 = b.nextInt();
double c = num1 * num1 + num2 * num2;
System.out.println(c);
a.close();
b.close();
}