【问题标题】:Java query to solveJava查询解决
【发布时间】:2016-11-28 08:46:06
【问题描述】:

这是问题:

给定一个声明为 double centerX=0, centerY=0 的圆,取 半径和坐标为 x, y 的点。编写一个程序 计算点是否在圆内(包括 边界)。

公式:

两点A(x1,y1)和B(x2,y2)的距离为:

((x2-x1)^2+(y2-y1)^2)的平方根

输入规范:

第一行将包含半径 r。

第二行包含 x 和 y。每个值都是 (x,y) 坐标 这样 x, y>0.

输出规格:

如果点在圆内打印“点 (x, y) 在圆内 圆”,否则打印“点(x,y)在圆外”。

示例输入 1:

7

2 5

示例输出 1:

点 (2, 5) 在圆内

示例输入 2:

2

9 4

示例输出 2:

点 (9, 4) 在圆外

解释:

在样本输入 1 中,(0,0) 和 (2,5) 之间的距离为 5.3851 小于半径 7。因此该点在圆内。 而在另一组输入中,该点位于圆外 因为原点和 (9,4) 之间的距离是 9.8488,即 大于给定半径 2。

现在这是我的代码:

import java.util.Scanner;
import java.math.*;
class Circ{
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int r=s.nextInt();
        int x=s.nextInt();
        int y=s.nextInt();
        if(x>0&&y>0){
            double dist=Math.sqrt(Math.pow(x-0,2)+Math.pow(y-0,2));
        if(dist>r){
            System.out.println("The point ("+x+","+y+") is outside the circle");
        }else if(dist<=r){
            System.out.println("The point ("+x+","+y+") is inside the circle");
        }
        }

    }
}

它给出了正确的输出但没有通过测试用例?

【问题讨论】:

  • JavaScriptJava 并不完全相同。请删除JavaScript 标签。
  • 你知道测试用例是什么吗?
  • 问题中给出了@marstran
  • 发布测试用例输入值

标签: java eclipse


【解决方案1】:
import java.util.Scanner;
public class circleOfCodeG{
public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int  r,x,y;
    double dis;
    r=sc.nextInt();
    x=sc.nextInt(); y=sc.nextInt();
    dis=Math.sqrt((x*x)+(y*y));
    if(dis<=r)
        System.out.println("The point ("+x+", "+y+") is inside the circle");
    else
        System.out.println("The point ("+x+", "+y+") is outside the circle");
}
}

试试这个。我敢肯定,这将满足“测试用例”。 :)

【讨论】:

    猜你喜欢
    • 2021-01-14
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2016-01-10
    • 2016-02-29
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多