Java基础练习3

  • 在这里插入图片描述
import java.util.*;
class class34{
    //定义一个数组进行单词帅选
    public static String[] words={"black","pink","blue","green"};
    //随机获取一个单词
    public static String word=null;
    //创建一个状态数组进行判断
    public static boolean[] sta=null;
    //定义一个输错的次数
    public static int missed=0;
    public static void main(String[] agre){
        Scanner scanner=new Scanner(System.in);
        Random random=new Random();
        word=words[random.nextInt(words.length)];
        sta=new boolean[word.length()];
        //用户输入字母,判断是否猜对
        while(true){
            String password=Guesspassword();            //获取现在的状态
            System.out.print("(Guess)Enter a letter in word "+password+" > ");
            String letter=scanner.nextLine();           //把用户猜测的单词放入letter进行判断
            changeword(letter);                         //通过letter,判断是否改变现有状态
            if(TheEnd()){
                System.out.println("The word is "+word+" . You missed "+missed+" times");
                System.out.println("Do you want to guess another word? Enter y or n>");
                if(scanner.nextLine().equals("y")){      //判断是否要重新来一局
                    //随机获取一个单词
                    word=words[random.nextInt(words.length)];
                    //创建一个状态数组进行判断
                    sta=new boolean[word.length()];
                    //定义一个输错的次数
                    missed=0;
                }else{
                    break;
                }
            }
        }
    }

    //获取当前状态的函数
    public static String Guesspassword(){
        String password="";
        for(int i=0;i<word.length();i++){          //依次遍历判断状态,并进行是否显示
            if(sta[i]==true){
                password+=word.charAt(i);
            }else{
                password+="*";
            }
        }
        return password;
    }

    //判断是否改变状态
    public static void changeword(String letter){
        //拿到输入单词,遍历当前状态,判断是否存在
        boolean flag=false;
        for(int i=0;i<word.length();i++){
            if((word.charAt(i)+"").equals(letter)){
                flag=true;
                if(sta[i]==false){
                    sta[i]=true;
                }else{
                    System.out.println("\t"+letter+" is already in the word");
                    return;
                }
            }
        }
        if(!flag){
            missed++;
            System.out.println("\t"+letter+" is not in the word");
        }

    }

    //判断是否终止,是否猜完
    public static boolean TheEnd(){
        //依次遍历,若均为true,则猜测完毕
        for(int i=0;i<sta.length;i++){
            if(sta[i]==false){
                return false;
            }
        }
        return true;
    }

}

  • 在这里插入图片描述
package eight;
import java.util.*;
public class eight_1 {
 
	public static void main(String[] args) {
		double[][] m=new double[10][10];//声明一个二维数组,二维数组实际上是一个其中每个元素都是一维数组的数组;
		/*如果想知道数组的长度,例如本题m.length表示的是行的数目,如果要表示列的数目就为m[0].length*/
		Scanner input=new Scanner(System.in);
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<4;j++)
			{
				m[i][j]=input.nextDouble();
			}
		}
		for(int i=0;i<4;i++)
		{
			double s=sumColumn(m,i);
			System.out.println("Sum of the elements at column " + i +" is " + s);
		}
		
 
	}
	public static double sumColumn(double[][] m, int n)
	{
		double sum=0;//定义以后要赋初值;
		for(int i=0;i<3;i++)
		{
			sum+=m[i][n];//这里不能在for里面定义sum即double sum+=m[i][n];因为这样定义sum是在for循环里面的局部变量,返回sum的值就会出错;
		}
		return sum;
	}
 
}
  • 在这里插入图片描述
import java.util.Scanner;

public class Test8_2 {
    public static void main(String[] args) {
        //1. 主方法:声明一个长度为4*4的double型矩阵
        double[][] m = new double[4][4];
        //2. 主方法:使用双层循环读取控制台输入为矩阵赋值
        System.out.println("Enter a 4-by-4 matrix row by row:");
        Scanner input = new Scanner(System.in);
        for (int a = 0 ; a < 4 ; a++){
            for (int b = 0 ; b < 4 ; b++){
                m[a][b] = input.nextDouble();
            }
        }
        //3. 主方法:创建一个double对象,调用sumMajorDiagonal方法接收方法返回值
        double result = sumMajorDiagonal(m);
        //4. 主方法:输出结果
        System.out.print("Sum of the elements in the major diagonal is " + result);
    }
    public static double sumMajorDiagonal(double[][] m){
        //5. sumMajorDiagonal方法:创建一个int型对象并赋值矩阵length,一个double对象用于保存加和
        int length = m.length;
        double sum = 0;
        //6. sumMajorDiagonal方法:使用循环读取对角线上元素的值
        for (int i = 0 ; i <length ; i++){
            sum += m[i][i];
        }
        //7. sumMajorDiagonal方法:返回加和结果
        return sum;
    }
}

  • 在这里插入图片描述
import java.util.Scanner;

public class Test8_5 {
    public static void main(String[] args) {
        //1. 主方法:声明3个3*3的二维数组(矩阵)
        double[][] a = new double[3][3];
        double[][] b = new double[3][3];
        double[][] c = new double[3][3];
        //2. 主方法:获取用户输入,输出提示语句,通过循环给第一个数组赋值
        Scanner input = new Scanner(System.in);
        System.out.print("Enter matrix1: ");
        for (int row = 0 ; row < a.length ; row++){
            for (int col = 0 ; col < a[0].length ; col++){
                a[row][col] = input.nextDouble();
            }
        }
        //3. 主方法:获取用户输入,输出提示语句,通过循环给第二个数组赋值
        System.out.print("Enter matrix2: ");
        for (int row = 0 ; row < b.length ; row++){
            for (int col = 0 ; col < b[0].length ; col++){
                b[row][col] = input.nextDouble();
            }
        }
        //4. 主方法:调用方法addMatrix,传入两个数组,用第三个数组接收返回值
        c = addMatrix(a, b);
        //5. 主方法:输出语句
        System.out.println("The matrices are added as follows");
        //6. 主方法:输出三个数组
        for (int row = 0 ; row < 3 ; row++){
            //打印数组a元素
            for (int col = 0 ; col < 3 ; col++){
                System.out.print(a[row][col] + " ");
            }
            //打印数组a与数组b之间的空格与+
            if (row != 1) {
                System.out.print("     ");
            } else {
                System.out.print("  +  ");
            }
            //打印数组b
            for (int col = 0 ; col < 3 ; col++){
                System.out.print(b[row][col] + " ");
            }
            //打印数组b与数组c之间的空格和=
            if (row != 1) {
                System.out.print("     ");
            } else {
                System.out.print("  =  ");
            }
            for (int col = 0 ; col < 3 ; col++){
                System.out.print(c[row][col] + " ");
            }
            System.out.println();
        }
    }
    public static double[][] addMatrix(double[][] a, double[][] b){
        //7. addMatrix方法:声明一个3*3的二维数组
        double[][] c = new double[a.length][a[0].length];
        //8. addMatrix方法:使用双层循环遍历传入的a和b两个数组,并将对应位置的元素相加并赋值给刚刚声明的数组
        for (int row = 0 ; row < c.length ; row++){
            for (int col = 0 ; col < c[0].length ; col++){
                c[row][col] = a[row][col] + b[row][col];
            }
        }
        //9. addMatrix方法:返回刚刚声明的数组
        return c;
    }
}

  • 在这里插入图片描述
import java.util.Scanner;

public class Test8_6 {
    public static void main(String[] args) {
        //1. 主方法:新建三个数组a、b、c,长度均为3*3
        double[][] a = new double[3][3];
        double[][] b = new double[3][3];
        double[][] c = new double[3][3];
        //2. 主方法:从控制台获取用户输入,使用循环读入数组a的元素
        Scanner input = new Scanner(System.in);
        System.out.print("Enter matrix1: ");
        for (int i = 0; i < 3 ; i++){
            for (int j = 0 ; j < 3; j++){
                a[i][j] = input.nextDouble();
            }
        }
        //3. 主方法:从控制台获取用户输入,使用循环读入数组b的元素
        System.out.print("Enter matrix2: ");
        for (int i = 0; i < 3 ; i++){
            for (int j = 0 ; j < 3; j++){
                b[i][j] = input.nextDouble();
            }
        }
        //4. 主方法:调用方法multiplyMatrix,使用c对象接收返回值
        c = multiplyMatrix(a, b);
        //5. 主方法:输出The multipication……句
        System.out.println("The multipication of the matrices is");
        //6. 主方法:调用自定义的输出方法,传入a、b、c三个数组
        output(a, b ,c);
    }
    public static double[][] multiplyMatrix(double[][] a, double[][] b){
        //7. multiplyMatrix方法:声明一个数组c,长度为3*3
        double[][] c = new double[3][3];
        //8. multiplyMatrix方法:使用嵌套循环计算a*b,用c接收,a的列数为n
        // c[i][j] = a[i][1] * b[1][j] + a[i][2] * b[2][j]+……+a[i][n]*b[n][j]
        for (int i = 0 ; i < 3 ; i++){
            for (int j = 0 ; j < 3 ; j++){
                for (int n = 0; n < 3 ; n++){
                    c[i][j] += a[i][n] * b[n][j];
                }
            }
        }
        //9. multiplyMatrix方法:返回数组c
        return c;
    }
    public static void output(double[][] a, double[][] b, double[][] c){
        //三层循环,最外层行第二层多个同层次循环
        for (int i = 0 ; i < 3; i++){
            //数组a
            for (int j = 0 ; j < 3 ; j++){
                System.out.print(a[i][j] + " ");
            }
            //数组a与数组b之间的空格或*
            if ( i == 1 ){
                System.out.print("\t*\t");
            }
            else{
                System.out.print("\t\t");
            }
            //数组b
            for (int j = 0 ; j < 3 ; j++){
                System.out.print(b[i][j] + " ");
            }
            //数组b与数组c
            if ( i == 1 ){
                System.out.print(" =\t");
            }
            else{
                System.out.print("\t");
            }
            //数组c
            for (int j = 0 ; j < 3 ; j++){
                System.out.printf("%.1f ", c[i][j]);
            }
            System.out.println();
        }
    }
}

  • 在这里插入图片描述
package blackbook.chapter8;

import java.util.Arrays;

public class Test8_7 {
    public static void main(String[] args) {
        //1. 主方法:创建声明初始化points数组,复制题目给定语句即可
        double[][] points = {
                {  -1,  0, 3},
                {  -1, -1, -1},
                {   4,   1, 1},
                {   2, 0.5, 9},
                { 3.5, 2, -1},
                {   3, 1.5, 3},
                {-1.5, 4, 2},
                { 5.5, 4, -0.5}
        };

        //2. 主方法:新建一个double对象用于存储最小值、两个一维数组用于存储最大值的两端坐标值,使用循环比较保存了所有点间距离的数组所有元素
        double temp_ij = 0;
        double temp_min = distance(points[0], points[1]);
        double[] min_point1 = new double[3];
        double[] min_point2 = new double[3];
        for (int i = 0 ; i < points.length - 1; i++){
            for (int j = i + 1 ; j < points.length ; j++){
                temp_ij = distance(points[i], points[j]);
                if ( temp_min > temp_ij ){
                    temp_min = temp_ij;
                    min_point1 = points[i];
                    min_point2 = points[j];
                }
            }
        }
        //5. 主方法:将上面的两个一维数组坐标值输出
        System.out.println(Arrays.toString(min_point1) + "\n" + Arrays.toString(min_point2));
    }
    //3. 自定义方法distance:接收传入的两个数组,作为两点的坐标
    public static double distance(double[] a, double[] b){
        //4. 自定义方法distance:将两个数组对应元素平方并求和,使用两点距离公式求出距离并返回
        double temp_distance = 0;
        for (int i = 0; i < a.length ; i++){
            temp_distance += Math.pow(b[i] - a[i], 2);
        }
        return Math.sqrt(temp_distance);
    }
}


  • 在这里插入图片描述
public class Test8_10 {
    public static void main(String[] args) {
        //1. 声明一个4*4矩阵
        int[][] array = new int[4][4];
        //2. 使用双层循环给矩阵赋随机值0和1((int)(Math.random()*2))并打印(如遇到列结束则换行,最后一行不换)
        for (int i = 0 ; i < 4 ; i++){
            for (int j = 0 ; j < 4; j++){
                array[i][j] = (int)(Math.random()*2);
                System.out.print(array[i][j]);
            }
            System.out.println();
        }
        //3. 初始化两个int型对象,分别保存1最多的行下标和1的个数;再初始化1个int型对象,用于临时保存1的个数
        int largest_row = 0;
        int count_row = 0;
        int temp_row = 0;
        //4. 使用循环找1最多的行(双层循环)
        for (int i = 0 ; i < 4 ; i++){
            temp_row = 0;
            for (int j = 0 ; j < 4; j++){
                if (array[i][j] == 1){
                    ++temp_row;
                }
            }
            if (count_row < temp_row){
                count_row = temp_row;
                largest_row = i;
            }
        }
        //5. 找1最多的列与3~4步相似,只需要修改循环即可
        int largest_col = 0;
        int count_col = 0;
        int temp_col = 0;
        for (int j = 0 ; j < 4 ; j++){
            temp_col = 0;
            for (int i = 0 ; i < 4; i++){
                if (array[i][j] == 1){
                    ++temp_col;
                }
            }
            if (count_col < temp_col){
                count_col = temp_col;
                largest_col = j;
            }
        }
        //6. 输出最多1的行、列
        System.out.println("The largest row index: " + largest_row);
        System.out.println("The largest column index: " + largest_col);
    }
}

  • 在这里插入图片描述
public class Test5_40 {
    public static void main(String[] args) {
        // 计数变量
        int count_face = 0;
        int count_back = 0;

        // 产生结果:0表正面1表反面
        for (int i = 1; i <= 1000000; i++){
            int result = (int)(Math.random() * 2);
            if (result == 0){count_face++;}
            else {count_back++;}
        }

        // 输出结果
        System.out.printf("正面个数是%d, 反面个数是%d", count_face, count_back);
    }
}

  • 在这里插入图片描述
import java.util.Scanner;

public class Test8_19 {
    public static void main(String[] args) {
        //1. 主方法:接收用户输入,使用int接收行数、列数
        Scanner input = new Scanner(System.in);
        System.out.println("请输入数组行数、列数:");
        int array_row = input.nextInt();
        int array_col = input.nextInt();
        //2. 主方法:声明一个二维数组,长度为刚刚接收的行数*列数
        int[][] array = new int[array_row][array_col];
        //3. 主方法:使用循环接收用户输入的数组元素
        System.out.println("请输入数组元素:");
        for (int i = 0 ; i < array_row ; i++){
            for (int j = 0 ; j < array_col ; j++){
                array[i][j] = input.nextInt();
            }
        }
        //4. 主方法:调用方法isConsecutiveFour,传入数组
        //5. 主方法:使用条件判断语句判断方法返回值,根据结果返回不同boolean值
        if ( isConsecutiveFour(array) ){
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
    public static boolean isConsecutiveFour(int[][] values){
        //6. 方法isConsecutiveFour:先获取values数组的行数、列数
        int row = values.length;
        int col = values[0].length;
        //7. 方法isConsecutiveFour:分别对数组的行、列进行遍历,寻找是否有重复元素;再对数组两条对角线进行遍历,看是否能找到
        // 行遍历:如果找到返回true
        for (int i = 0 ; i < row ; i++){
            for (int j = 0 ; j < col - 3 ; j++){
                if ( values[i][j] == values[i][j+1] && values[i][j+1] == values[i][j+2]
                        && values[i][j+2] == values[i][j+3])
                    return true;
            }
        }
        // 列遍历
        for (int j = 0 ; j < col ; j++){
            for (int i = 0 ; i < row - 3 ; i++){
                if ( values[i][j] == values[i+1][j] && values[i+1][j] == values[i+2][j]
                        && values[i+2][j]== values[i+3][j])
                    return true;
            }
        }
        // 左下到右上的对角线
        for (int i = 3 ; i < row ; i++){
            for (int j = 0 ; j < col - 3 ; j++){
                if (values[i][j] == values[i-1][j+1] && values[i-1][j+1] == values[i-2][j+2]
                        && values[i-2][j+2]== values[i-3][j+3])
                    return true;
            }
        }
        // 左上到右下的对角线
        for (int i = 0 ; i < row - 3 ; i++){
            for (int j = 0 ; j < col - 3 ; j++){
                if (values[i][j] == values[i+1][j+1] && values[i+1][j+1] == values[i+2][j+2]
                        && values[i+2][j+2] == values[i+3][j+3])
                    return true;
            }
        }
        //8. 方法isConsecutiveFour:如果找到返回true,找不到返回false
        return false;
    }
}

  • 在这里插入图片描述
import java.util.Scanner;

public class Test8_20 {
    public static void main(String[] args) {
        //1. 主方法:先声明一个数组array,长度为6*7,类型为int(0表没有子,1表Y黄子,2表R红子)
        int[][] array = new int[6][7];
        //2. 主方法:创建一个boolean对象bool,初始值为true
        boolean bool = true;
        //3. 主方法:创建一个int型对象temp(用于临时存储变量)1个int型对象result,存储结果
        int temp = 0, result = 0;
        //4. 主方法:创建一个计数变量count,int类型(初始值为0)
        int count = 0;
        //5. 主方法:接收用户输入语句
        Scanner input = new Scanner(System.in);
        //6. 主方法:循环体:调用打印棋盘状态方法,传入数组array
        print_array(array);
        //7. 主方法:使用循环运行这个游戏,继续运行条件为bool
        while (bool) {
            //8. 主方法:循环体:计数变量count+1
            ++count;
            //9. 主方法:循环体:接收用户的输入,使用temp接收
            if (count % 2 == 1)
                System.out.print("Drop a red disk at column (0-6): ");
            else
                System.out.print("Drop a yellow disk at column (0-6): ");
            //10. 主方法:循环体:调用棋盘状态改变方法,传入array、temp与count(对棋盘状态进行改变),使用array接收
            temp = input.nextInt();
            array = change_array(array, temp, count);
            //11. 主方法:循环体:再调用打印棋盘状态的方法,传入数组array
            print_array(array);
            //12. 主方法:循环体:调用结果判断方法,传入数组array,result接收
            // 返回值为1则将bool改为false并输出黄色玩家赢,返回值2将bool改为false并输出红色玩家赢,返回值为0则继续
            result = judge_array(array);
            if (result == 1){
                System.out.print("The yellow player won");
                bool = false;
            } else if (result == 2) {
                System.out.print("The red player won");
                bool = false;
            }
        }
    }
    //13. 打印棋盘状态方法:传入数组array,无返回值
    public static void print_array(int[][] array){
        //14. 打印棋盘状态方法:对数组array进行遍历
        // 0则输出|+" ",1则输出| + "Y",2则输出| + " R",每行结尾打印一个|并换行
        for (int i = 0 ; i <array.length ; i++){
            for (int j = 0 ; j < array[i].length ;j++){
                if ( array[i][j] == 0 ){
                    System.out.print("| ");
                } else if ( array[i][j] == 1 ){
                    System.out.print("|Y");
                } else {
                    System.out.print("|R");
                }
            }
            System.out.println("|");
        }
    }
    //15. 棋盘状态改变方法:传入int型二维数组array、表示新元素所在列的temp和计数变量count,返回int型二维数组
    public static int[][] change_array(int[][] array, int temp, int count){
        //16. 棋盘状态改变方法:对temp列从下到上遍历,找到元素值为0的地方并替换为(count % 2 + 1)
        for (int i = array.length - 1 ; i >= 0 ; i--){
            if ( array[i][temp] == 0 ){
                array[i][temp] = count % 2 + 1;
                return array;
            }
        }
        //17. 棋盘状态改变方法:返回新数组
        return array;
    }
    //18. 结果判断方法:接收二维数组,返回int型值
    public static int judge_array(int[][] array){
        //19. 结果判断方法:对数组进行行遍历、列遍历、对角线遍历
        // 当有连续四个值相同且均为1或2时,如果值均为1则返回1、值均为2则返回2
        // 行遍历部分
        for (int i = 0 ; i < array.length ; i++){
            for (int j = 0 ; j < array[i].length - 3 ; j++){
                if ( array[i][j] == array[i][j+1] && array[i][j+1] == array[i][j+2]
                        && array[i][j+2] == array[i][j+3] ){
                    if (array[i][j] == 1){
                        return 1;
                    }
                    if (array[i][j] == 2){
                        return 2;
                    }
                }
            }
        }
        // 列遍历
        for (int j = 0 ; j < array[0].length ; j++){
            for (int i = 0 ; i < array.length - 3 ; i++){
                if (array[i][j] == array[i+1][j] && array[i+1][j] == array[i+2][j]
                        && array[i+2][j] == array[i+3][j]){
                    if (array[i][j] == 1){
                        return 1;
                    }
                    if (array[i][j] == 2){
                        return 2;
                    }
                }
            }
        }
        // 对角线:左上到右下
        for (int i = 0 ; i < array.length - 3 ; i++){
            for (int j = 0 ; j < array[i].length - 3 ;j++){
                if (array[i][j] == array[i+1][j+1] && array[i+1][j+1] == array[i+2][j+2]
                        && array[i+2][j+2] == array[i+3][j+3]){
                    if (array[i][j] == 1){
                        return 1;
                    }
                    if (array[i][j] == 2){
                        return 2;
                    }
                }
            }
        }
        // 对角线:左下到右上
        for (int i = array.length - 1 ; i >= 3 ; i--){
            for (int j = 0 ; j < array[i].length - 3 ; j++){
                if (array[i][j] == array[i-1][j+1] && array[i-1][j+1] == array[i-2][j+2]
                        && array[i-2][j+2] == array[i-3][j+3]){
                    if (array[i][j] == 1){
                        return 1;
                    }
                    if (array[i][j] == 2){
                        return 2;
                    }
                }
            }
        }
        //20. 结果判断方法:如果上面情况都没有执行到返回0
        return 0;
    }
}

  • 在这里插入图片描述
package chapter08;

import java.util.Scanner;

public class Code_25 {
    public static void main(String[] args) {
        Scanner input =new Scanner(System.in);
        System.out.println("Enter a 3-by-3 matrix row by row:");
        double[][] matrix=new double[3][3];

        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[i].length;j++){
                matrix[i][j]=input.nextDouble();
            }
        }

        if (isMarkovMatrix(matrix))
            System.out.println("It is a Markov matrix");
        else System.out.println("It is not a Markov matrix");
    }
    public static boolean isMarkovMatrix(double[][] m){
        double sum=0;

        for(int i=0;i<m.length;i++){
            for(int j=0;j<m[i].length;j++){
                if (m[i][j]<0)
                    return false;
            }
        }
        for(int j=0;j<m[0].length;j++){
            for(int i=0;i<m.length;i++){
                sum+=m[i][j];
            }
            if (sum==1)
                return true;
        }
        return false;
    }
}


  • 在这里插入图片描述
import java.util.Scanner;

public class Test8_32 {
    public static void main(String[] args) {
        //1. 主方法:声明一个3*2的二维数组points,double型
        double[][] points = new double[3][2];
        //2. 主方法:输出提示输入语句,使用循环接收点坐标
        Scanner input = new Scanner(System.in);
        System.out.print("Enter x1, y1, x2, y2, x3, y3: ");
        for (int i = 0 ; i < 3 ; i++){
            for (int j = 0 ; j < 2 ; j++){
                points[i][j] = input.nextDouble();
            }
        }
        //3. 主方法:调用自定义方法getTriangleArea,传入二维数组points
        double feedback = getTriangleArea(points);
        //4. 主方法:根据返回值输出对应内容
        if ( feedback == 0){
            System.out.println("The three points are on the same line");
        } else {
            System.out.printf("The area of the triangle is %.2f", feedback);
        }
    }
    public static double getTriangleArea(double[][] points){
        //5. 方法getTriangleArea:先判断三个点是否在同一条直线上,如果在则返回0
        double k01 = 0 , k02 = 0, b01 = 0, b02 = 0;
        k01 = (points[1][1] - points[0][1] ) / (points[1][0] - points[0][0]);
        k02 = (points[2][1] - points[0][1]) / (points[2][0] - points[0][0]);
        b01 = points[0][1] - k01 * points[0][0];
        b02 = points[0][1] - k02 * points[0][0];
        if (k01==k02 && b01==b02){
            return 0;
        }
        //6. 方法getTriangleArea:计算三条边
        double side1 = Math.sqrt(Math.pow(points[1][0] - points[0][0], 2) + Math.pow(points[1][1] - points[0][1], 2));
        double side2 = Math.sqrt(Math.pow(points[2][0] - points[0][0], 2) + Math.pow(points[2][1] - points[0][1], 2));
        double side3 = Math.sqrt(Math.pow(points[2][0] - points[1][0], 2) + Math.pow(points[2][1] - points[1][1], 2));
        //7. 方法getTriangleArea:计算公式中的s
        double s = (side1 + side2 + side3) / 2;
        //8. 方法getTriangleArea:返回面积
        return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
    }
}

  • 在这里插入图片描述
import java.util.Arrays;
import java.util.Scanner;

public class Test8_33 {
    public static void main(String[] args) {
        //1. 获取四个点的坐标,使用数组读入
        double[][] array = new double[4][2];
        Scanner input = new Scanner(System.in);
        System.out.println("Enter x1, y1, x2, y2, x3, y3, x4, y4:");
        for (int i = 0 ; i < 4 ; i++){
            for (int j = 0 ; j < 2 ; j++){
                array[i][j] = input.nextDouble();
            }
        }
        //2. 求v1v3和v2v4线的交点坐标
        double[] loc = intersection(array);
        //3. 自定义方法求三角形面积(传入数组、下标、交点坐标数组),使用数组接收
        double[] area = new double[4];
        for (int i = 0 ; i < 4 ; i++){
            area[i] = triangle(array, i, loc);
        }
        //4. 对数组排序,输出
        Arrays.sort(area);
        System.out.print("The areas are ");
        for (int i = 0 ; i < 4 ; i++){
            System.out.printf("%.2f ", area[i]);
        }
    }

    /** 求v1v3和v2v4线的交点坐标 */
    public static double[] intersection(double[][] arr){
        double k02 = (arr[2][1] -arr[0][1]) / (arr[2][0] - arr[0][0]);
        double k13 = (arr[3][1] -arr[1][1]) / (arr[3][0] - arr[1][0]);
        double b02 = arr[0][1] - k02 * arr[0][0];
        double b13 = arr[1][1] - k13 * arr[1][0];

        double x = (b02 - b13) / (k13 - k02);
        double y = (b02 * k13 - b13 * k02) / (k13 - k02);

        double[] feedback = {x, y};
        return feedback;
    }

    /** 求三角形面积 */
    public static double triangle(double[][] arr0, int index0, double[] arr1){
        int index1 = (index0 + 1) % 4;

        double side1 = Math.sqrt(Math.pow(arr0[index1][0] - arr0[index0][0], 2) + Math.pow(arr0[index1][1] - arr0[index0][1], 2));
        double side2 = Math.sqrt(Math.pow(arr1[0] - arr0[index0][0], 2) + Math.pow(arr1[1] - arr0[index0][1], 2));
        double side3 = Math.sqrt(Math.pow(arr1[0] - arr0[index1][0], 2) + Math.pow(arr1[1] - arr0[index1][1], 2));

        double s = (side1 + side2 + side3) / 2;

        return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发热的嘤嘤怪(2003计科胜胜同学)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值