一 Java博客作业( 三 )


例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong " 。
如果符合基本格式,但输入点的数量不符合要求,输出"wrongof " 。
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出" ",
输出格式:
见题目描述 。
类图:
大概思路:
通过legal函用来判断输入数据是否合法,把选项看作一个类,具体写法和第一题差不多,但写到这时候发现用一个函数把字符串转换成数字类型太麻烦,直接强制转换,正负号也可以保留下来 。
这道题复杂的是算法
计算斜率:k=(y2-y1)/(x2-x1);
计算一点到另外两点构成的直线的距离:需要先算直线方程,再求点到该直线的距离 :
判断三点是否在一线,即判断三点中任意两点组成的线段的斜率是否相等,这个比较简单,用(y1-y2)*(x-x2)==(x1-x2)*(y-y2)判断即可
判断两条线是否平行,即判断斜率是否存在且相等,比较两条线k=(y2-y1)/(x2-x1);
判断两线关系,如果不平行,则计算交点位置,并判断交点是否在线上 。交点公式参考文章:两点确定一条直线,已知四个点确定的两条直线,求这两条直线的交点的博客-CSDN博客
源码(具体代码分析写在注释里):
import java.util.Scanner;public class Main{public static void main(String[] args) {Scanner s = new Scanner(System.in);String str = s.nextLine();P z = new P();boolean a =false;for(int i=0;i 1 && x.charAt(1) != '.')return false;}if (x.matches(w))return true;elsereturn false;}void One(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");String[] point2 = dj[1].split(",");if (!dj[0].equals(dj[1])) {//防止点重合if (point1.length == 2 && point2.length == 2) {//判断有x,y的值if (legal(point1[0]) && legal(point1[1]) && legal(point2[0]) && legal(point2[1])) {//判断输入数据合法double x1 = Double.parseDouble(point1[0]);double y1 = Double.parseDouble(point1[1]);double x2 = Double.parseDouble(point2[0]);double y2 = Double.parseDouble(point2[1]);if (dj.length == 2)if (x1 == x2)System.out.print("Slope does not exist");elseSystem.out.print((y2 - y1) / (x2 - x1));//一切正确,输出斜率if (dj.length > 2)System.out.print("wrong number of points");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("points coincide");}void Two(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");String[] point2 = dj[1].split(",");if (dj.length < 3)//防止点数量不够,导致不存在后面的元素System.out.print("wrong number of points");String[] point3 = dj[2].split(",");if (!dj[1].equals(dj[2])) {if (point1.length == 2 && point2.length == 2 && point3.length == 2) {if (legal(point1[0]) && legal(point1[1]) && legal(point2[0]) && legal(point2[1]) && legal(point3[0])&& legal(point3[1])) {double x0 = Double.parseDouble(point1[0]);//将输入的子字符串转换成数据double y0 = Double.parseDouble(point1[1]);double x1 = Double.parseDouble(point2[0]);double y1 = Double.parseDouble(point2[1]);double x2 = Double.parseDouble(point3[0]);double y2 = Double.parseDouble(point3[1]);if (dj.length == 3)System.out.print(Math.abs((y2 - y1) * (x0 - x1) - (x2 - x1) * (y0 - y1))//点到直线的公式/ Math.sqrt(Math.pow((y1 - y2), 2) + Math.pow((x1 - x2), 2)));if (dj.length > 3)System.out.print("wrong number of points");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("points coincide");}void Three(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");if (dj.length < 3)System.out.print("wrong number of points");String[] point2 = dj[1].split(",");String[] point3 = dj[2].split(",");if (!dj[1].equals(dj[2])&& !dj[0].equals(dj[2])&& !dj[0].equals(dj[1])) {if (point1.length == 2 && point2.length == 2 && point3.length == 2) {if (legal(point1[0]) && legal(point1[1]) && legal(point2[0]) && legal(point2[1]) && legal(point3[0])&& legal(point3[1])) {double x0 = Double.parseDouble(point1[0]);double y0 = Double.parseDouble(point1[1]);double x1 = Double.parseDouble(point2[0]);double y1 = Double.parseDouble(point2[1]);double x2 = Double.parseDouble(point3[0]);double y2 = Double.parseDouble(point3[1]);if (dj.length == 3 &&((y2- y1) / (x0 - x1) == (y0 - y1) / (x2 - x1)||(y0- y1) / (x2 - x1) == (y2 - y1) / (x0 - x1)))//判断三点是否一线System.out.print("true");if ((y2- y1) / (x0 - x1) != (y0 - y1) / (x2 - x1)||(y0- y1) / (x2 - x1) != (y2 - y1) / (x0 - x1))System.out.print("false");if (dj.length > 3)System.out.print("wrong number of points");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("points coincide");}void Four(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");String[] point2 = dj[1].split(",");if (dj.length