importjava.util.*;class class34{//定义一个数组进行单词帅选publicstaticString[] words={"black","pink","blue","green"};//随机获取一个单词publicstaticString word=null;//创建一个状态数组进行判断publicstaticboolean[] sta=null;//定义一个输错的次数publicstaticint missed=0;publicstaticvoidmain(String[] agre){Scanner scanner=newScanner(System.in);Random random=newRandom();
word=words[random.nextInt(words.length)];
sta=newboolean[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=newboolean[word.length()];//定义一个输错的次数
missed=0;}else{break;}}}}//获取当前状态的函数publicstaticStringGuesspassword(){String password="";for(int i=0;i<word.length();i++){//依次遍历判断状态,并进行是否显示if(sta[i]==true){
password+=word.charAt(i);}else{
password+="*";}}return password;}//判断是否改变状态publicstaticvoidchangeword(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");}}//判断是否终止,是否猜完publicstaticbooleanTheEnd(){//依次遍历,若均为true,则猜测完毕for(int i=0;i<sta.length;i++){if(sta[i]==false){returnfalse;}}returntrue;}}
packageeight;importjava.util.*;publicclass eight_1 {publicstaticvoidmain(String[] args){double[][] m=newdouble[10][10];//声明一个二维数组,二维数组实际上是一个其中每个元素都是一维数组的数组;/*如果想知道数组的长度,例如本题m.length表示的是行的数目,如果要表示列的数目就为m[0].length*/Scanner input=newScanner(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);}}publicstaticdoublesumColumn(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;}}
importjava.util.Scanner;publicclassTest8_2{publicstaticvoidmain(String[] args){//1. 主方法:声明一个长度为4*4的double型矩阵double[][] m =newdouble[4][4];//2. 主方法:使用双层循环读取控制台输入为矩阵赋值System.out.println("Enter a 4-by-4 matrix row by row:");Scanner input =newScanner(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);}publicstaticdoublesumMajorDiagonal(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;}}
importjava.util.Scanner;publicclassTest8_5{publicstaticvoidmain(String[] args){//1. 主方法:声明3个3*3的二维数组(矩阵)double[][] a =newdouble[3][3];double[][] b =newdouble[3][3];double[][] c =newdouble[3][3];//2. 主方法:获取用户输入,输出提示语句,通过循环给第一个数组赋值Scanner input =newScanner(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(" + ");}//打印数组bfor(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();}}publicstaticdouble[][]addMatrix(double[][] a,double[][] b){//7. addMatrix方法:声明一个3*3的二维数组double[][] c =newdouble[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;}}
importjava.util.Scanner;publicclassTest8_6{publicstaticvoidmain(String[] args){//1. 主方法:新建三个数组a、b、c,长度均为3*3double[][] a =newdouble[3][3];double[][] b =newdouble[3][3];double[][] c =newdouble[3][3];//2. 主方法:从控制台获取用户输入,使用循环读入数组a的元素Scanner input =newScanner(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);}publicstaticdouble[][]multiplyMatrix(double[][] a,double[][] b){//7. multiplyMatrix方法:声明一个数组c,长度为3*3double[][] c =newdouble[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方法:返回数组creturn c;}publicstaticvoidoutput(double[][] a,double[][] b,double[][] c){//三层循环,最外层行第二层多个同层次循环for(int i =0; i <3; i++){//数组afor(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");}//数组bfor(int j =0; j <3; j++){System.out.print(b[i][j]+" ");}//数组b与数组cif( i ==1){System.out.print(" =\t");}else{System.out.print("\t");}//数组cfor(int j =0; j <3; j++){System.out.printf("%.1f ", c[i][j]);}System.out.println();}}}
publicclassTest5_40{publicstaticvoidmain(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);}}
importjava.util.Scanner;publicclassTest8_19{publicstaticvoidmain(String[] args){//1. 主方法:接收用户输入,使用int接收行数、列数Scanner input =newScanner(System.in);System.out.println("请输入数组行数、列数:");int array_row = input.nextInt();int array_col = input.nextInt();//2. 主方法:声明一个二维数组,长度为刚刚接收的行数*列数int[][] array =newint[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");}}publicstaticbooleanisConsecutiveFour(int[][] values){//6. 方法isConsecutiveFour:先获取values数组的行数、列数int row = values.length;int col = values[0].length;//7. 方法isConsecutiveFour:分别对数组的行、列进行遍历,寻找是否有重复元素;再对数组两条对角线进行遍历,看是否能找到// 行遍历:如果找到返回truefor(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])returntrue;}}// 列遍历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])returntrue;}}// 左下到右上的对角线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])returntrue;}}// 左上到右下的对角线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])returntrue;}}//8. 方法isConsecutiveFour:如果找到返回true,找不到返回falsereturnfalse;}}
importjava.util.Scanner;publicclassTest8_20{publicstaticvoidmain(String[] args){//1. 主方法:先声明一个数组array,长度为6*7,类型为int(0表没有子,1表Y黄子,2表R红子)int[][] array =newint[6][7];//2. 主方法:创建一个boolean对象bool,初始值为trueboolean bool =true;//3. 主方法:创建一个int型对象temp(用于临时存储变量)1个int型对象result,存储结果int temp =0, result =0;//4. 主方法:创建一个计数变量count,int类型(初始值为0)int count =0;//5. 主方法:接收用户输入语句Scanner input =newScanner(System.in);//6. 主方法:循环体:调用打印棋盘状态方法,传入数组arrayprint_array(array);//7. 主方法:使用循环运行这个游戏,继续运行条件为boolwhile(bool){//8. 主方法:循环体:计数变量count+1++count;//9. 主方法:循环体:接收用户的输入,使用temp接收if(count %2==1)System.out.print("Drop a red disk at column (0-6): ");elseSystem.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. 主方法:循环体:再调用打印棋盘状态的方法,传入数组arrayprint_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;}elseif(result ==2){System.out.print("The red player won");
bool =false;}}}//13. 打印棋盘状态方法:传入数组array,无返回值publicstaticvoidprint_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("| ");}elseif( array[i][j]==1){System.out.print("|Y");}else{System.out.print("|R");}}System.out.println("|");}}//15. 棋盘状态改变方法:传入int型二维数组array、表示新元素所在列的temp和计数变量count,返回int型二维数组publicstaticint[][]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型值publicstaticintjudge_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){return1;}if(array[i][j]==2){return2;}}}}// 列遍历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){return1;}if(array[i][j]==2){return2;}}}}// 对角线:左上到右下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){return1;}if(array[i][j]==2){return2;}}}}// 对角线:左下到右上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){return1;}if(array[i][j]==2){return2;}}}}//20. 结果判断方法:如果上面情况都没有执行到返回0return0;}}
packagechapter08;importjava.util.Scanner;publicclassCode_25{publicstaticvoidmain(String[] args){Scanner input =newScanner(System.in);System.out.println("Enter a 3-by-3 matrix row by row:");double[][] matrix=newdouble[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");elseSystem.out.println("It is not a Markov matrix");}publicstaticbooleanisMarkovMatrix(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)returnfalse;}}for(int j=0;j<m[0].length;j++){for(int i=0;i<m.length;i++){
sum+=m[i][j];}if(sum==1)returntrue;}returnfalse;}}
importjava.util.Scanner;publicclassTest8_32{publicstaticvoidmain(String[] args){//1. 主方法:声明一个3*2的二维数组points,double型double[][] points =newdouble[3][2];//2. 主方法:输出提示输入语句,使用循环接收点坐标Scanner input =newScanner(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,传入二维数组pointsdouble 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);}}publicstaticdoublegetTriangleArea(double[][] points){//5. 方法getTriangleArea:先判断三个点是否在同一条直线上,如果在则返回0double 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){return0;}//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:计算公式中的sdouble s =(side1 + side2 + side3)/2;//8. 方法getTriangleArea:返回面积returnMath.sqrt(s*(s-side1)*(s-side2)*(s-side3));}}
importjava.util.Arrays;importjava.util.Scanner;publicclassTest8_33{publicstaticvoidmain(String[] args){//1. 获取四个点的坐标,使用数组读入double[][] array =newdouble[4][2];Scanner input =newScanner(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 =newdouble[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线的交点坐标 */publicstaticdouble[]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;}/** 求三角形面积 */publicstaticdoubletriangle(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;returnMath.sqrt(s*(s-side1)*(s-side2)*(s-side3));}}