试验10
练习一矩阵
package test1;
public class juzhen {
public static double[][] mult(double[][] a,double[][] b){
int ahang = a.length;
int alie = a[0].length;
int bhang = b.length;
int blie = b[0].length;
double[][] c = new double[ahang][blie];
if(alie==bhang){
for(int i =0;i<ahang;i++){
for(int j=0;j<blie;j++){
double sum=0;
for(int k=0;k<alie;k++){
sum = sum+(a[i][k]*b[k][j]);
}
c[i][j]=sum;
}
}
}
else{
System.out.println("行列数不能相乘");
}
return c;
}
public static void main(String [] args){
double[][] a= {{1,2,3},{4,5,6},{7,8,9}};
double[][] b={{1,2,3},{3,4,5},{5,6,7}};
System.out.println("数组a");
for(int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
System.out.print(a[i][j]+",");
}
System.out.println();
}
System.out.println("数组b");
for(int i=0;i<b.length;i++){
for(int j=0;j<b[0].length;j++){
System.out.print(b[i][j]+",");
}
System.out.println();
}
System.out.println("相乘之后");
double[][]c = mult(a, b);
for(int i=0;i<c.length;i++){
for(int j=0;j<c[0].length;j++){
System.out.print(c[i][j]+",");
}
System.out.println();
}
}
}
练习二焦点
package test2;
import java.util.Scanner;
public class test {
public static void main(String[] args){
System.out.println("请输入a1, b1, a2, b2, c1, d1, c2, d2");
Scanner scan = new Scanner(System.in);
double a1=scan.nextDouble();
double b1=scan.nextDouble();
double a2=scan.nextDouble();
double b2=scan.nextDouble();
double c1=scan.nextDouble();
double d1=scan.nextDouble();
double c2=scan.nextDouble();
double d2=scan.nextDouble();
tools t = new tools();
t.tol(a1, b1, a2, b2, c1, d1, c2, d2);
}
}
package test2;
public class tools {
public boolean init(double a1,double b1,double a2,double b2,double x){
if(x>=a1&&x<=a2||x>=a2&&x<=a1){
return true;
}
else{
return false;
}
}
public void tol(double a1,double b1,double a2,double b2,double c1,double d1,double c2,double d2){
double x=((a2-a1)*(c2-c1)*(d2-b2)+(b2-b1)*(c2-c1)*a2-(d2-d1)*(a2-a1)*c2)/((b2-b1)*(c2-c1)-(d2-d1)*(a2-a1));
double y=(b2-b1)/(a2-a1)*(x-a2)+b2;
if(init(a1,b1,a2,b2,x)!=true||init(c1,d1,c2,d2,x)!=true){
System.out.println("无焦点");
}
else if((a2-a1)*(x-a2)==0||((b2-b1)*(c2-c1)-(d2-d1)*(a2-a1))==0)
{
System.out.println("无焦点");
} else{
System.out.println("交点");
System.out.println("x="+x);
System.out.println("y="+y);
}
}
}