一 Java博客作业( 五 )


输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y 。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔 。
输出格式:
基本输出格式见每种选项的描述 。
异常情况输出:
如果不符合基本格式,输出"Wrong " 。
如果符合基本格式,但输入点的数量不符合要求,输出"wrongof " 。
如果输入的三个点无法构成三角形,输出"data error" 。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位 。小数点后若不足6位,按原始位数显示,不必补齐 。例如:1/3的结果按格式输出为 0.,1.0按格式输出为1.0
选项4中所输入线的两个点坐标重合,输出" ",
类图
大概思路:
数据输入的判断同前两题一样,但难度相对上一题进一步升级,要对面的问题进行判断,有一定难度的提升
源码(具体代码分析写在注释里):
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;}String tran(double x) {String str_d = String.valueOf(x);str_d = str_d.substring(str_d.indexOf(".") + 1);int len = str_d.length();len = len > 6 ? 6 : len;String out = String.format("%." + len + "f", x);return out;}void One(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 (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]);double a = Math.sqrt(Math.pow(y1 - y0, 2) + Math.pow(x1 - x0, 2));double b = Math.sqrt(Math.pow(y2 - y0, 2) + Math.pow(x2 - x0, 2));double c = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));if (dj.length == 3 && a + b > c && a + c > b && b + c > a) {if (a == b || b == c || a == c)System.out.print("true ");elseSystem.out.print("false ");if (a == b && b == c)System.out.print("true ");elseSystem.out.print("false");}elseSystem.out.print("data error");if (dj.length > 3)System.out.print("wrong number of points");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("Wrong Format");}void Two(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 (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]);double a = Math.sqrt(Math.pow(y1 - y0, 2) + Math.pow(x1 - x0, 2));double b = Math.sqrt(Math.pow(y2 - y0, 2) + Math.pow(x2 - x0, 2));double c = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));if (dj.length == 3 && a + b > c && a + c > b && b + c > a) {System.out.print(tran(a + b + c) + " "+ tran(0.5 * c * Math.abs((y2 - y1) * (x0 - x1) - (x2 - x1) * (y0 - y1))/ Math.sqrt(Math.pow((y1 - y2), 2) + Math.pow((x1 - x2), 2)))+ " " + tran((x0 + x1 + x2) / 3) + "," + tran((y0 + y1 + y2) / 3));} elseSystem.out.print("data error");if (dj.length > 3)System.out.print("wrong number of points");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("Wrong Format");}void Three(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");if (dj.length