简体   繁体   English

二进制运算符'-'的错误操作数类型

[英]bad operand types for binary operator '-'

I'm getting an error message on line "Math.sqrt(Math.pow ((x2)-(x1), 2)+Math.pow((y2)-(y1), 2));" 我在“ Math.sqrt(Math.pow((x2)-(x1),2)+ Math.pow((y2)-(y1),2));上收到一条错误消息。” and I can't figure what the problem is. 我不知道是什么问题。

import java.util.Scanner;
import java.lang.Object;

public class Distance

{

   public static void main(String[] args)

   { 
    Scanner scan = new Scanner(System.in);

    System.out.println ("Enter x1: ");
    String x1=scan.nextLine();

    System.out.println ("Enter y1: ");
    String y1=scan.nextLine();

    System.out.println ("Enter x2: ");
    String x2=scan.nextLine();

    System.out.println ("Enter y2: ");
    String y2=scan.nextLine();

    double distance = Math.sqrt(Math.pow ((x2)-(x1), 2)+Math.pow((y2)-(y1), 2));

    System.out.println ("The distance between the points is: "+ distance);

   }

}

You're subtracting String s, not int s. 您要减去String ,而不是int

Parse the String s with Integer.parseInt(x1) . 使用Integer.parseInt(x1)解析String

- does not exist for type String . -类型String不存在。

You probably entered int values that you can read doing: 您可能输入了可以读取的int值:

int x1 = scan.nextInt();
// etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM