《算法基础课我的最爱》==》第一课
package com.Ant_1; public class Demo_3 { public static void main (String[]args) { for(int A =12;A<50;A=A+4) { System.out.println(A); System.out.println("=========================="); } //声明的方法构造 //数值的运算法则 + - * / % sum(111,222); average(111,222,333,444); } public static void sum(int a,int b){ long c =a+b; long s=a*b*a; long d=a*a*a; long e=c*s/d; long h=e*d*c*s; long n=c-s; System.out.println(+c); System.out.println("++++++++++++++++++++++++"); System.out.println(+s); System.out.println(+d); System.out.println(+e); System.out.println(+h); System.out.println(+n); } public static void average(int s,int v,int y, int u){ long ab=s+v; long cv =s+v+y+u/4; long bn =s*s*s; long mk =s+v+y*u; System.out.println(+ab); System.out.print("------------------"); System.out.println(+cv); System.out.println("============================="); System.out.println(+bn); System.out.println(+mk); System.out.println(+mk/4); //3 yutArea(2); // styQut(1); } public static void yutArea(double r){ double area =Math.PI*r*r; System.out.println(+area); } public static void styQut(double r){ double area =Math.PI*r*r; System.out.println(+area); class max{ public max() { max max=new max(); for(int a =12;a<50;a=a+4) { System.out.println(a); System.out.println("=========================="); } } } }
package com.Ant_1; /** * 流程控制的内容代码 * @author MZFAITHDREAM * */ public class Demo_4 { public static void main (String[]args) { //1 if语句 { int A =25; int B=23; if(A>B); System.out.println(A>B); } //2 if 语句 { int A =100; int B=50; if(A>B); System.out.println("A"); }if(true) { System.out.println("B"); } //3 for语句循环 { for(int s =2;s<4;s=s+3) { System.out.println(s); //4 do while语句循环 { int y =1; int sum =0; do { System.out.println(y); ++y; }while(y<10); System.out.println(sum); System.out.println(sum/10); } } } sum(111,222); average(111,222,333,444); } public static void sum(int a,int b){ long c =a+b; long s=a*b*a; long d=a*a*a; long e=c*s/d; long h=e*d*c*s; long n=c-s; System.out.println(+c); System.out.println(+s); System.out.println(+d); System.out.println(+e); System.out.println(+h); System.out.println(+n); } public static void average(int s,int v,int y, int u){ long ab=s+v; long cv =s+v+y+u/4; long bn =s*s*s; long mk =s+v+y*u; System.out.println(+ab); System.out.println(+cv); System.out.println(+bn); System.out.println(+mk); System.out.println(+mk/4); } }
package com.Ant_1; /** * 流程控制的内容代码二 * @author MZFAITHDREAM * */ public class Demo_6 { public static void main(String[] args) { // TODO Auto-generated method stub //1 int i = 10; while(i>=0){ System.out.println(i); --i; } //2 for(int a =1;a<=10;a =a+1){ System.out.println(a); } //3 int a = 1; while (a<=1000){ if(a%2==0){ System.out.println(a); } a++; } //4 int f=1; do{ System.out.println(f); ++f; }while(f<=100); //5 int h =1; int j =1; do{ System.out.println(j); j =j+4; h++; }while(h<=50); //6 int t =1; int sum =0; do{ System.out.println(t); sum =sum+t; ++t; }while(t<=100); System.out.println(sum); } }
package com.Ant_1; import java.util.Scanner; /** * 综合运用 * @author MZFAITHDREAM * */ public class Demo_8 { public static void main (String[]args) { Scanner sc = new Scanner (System.in); System.out.print("请输入你的消费金额:"); double sMoney = sc.nextDouble(); if (sMoney<=0) { System.out.println("你输入有!!!!!"); }else if(sMoney<50) { System.out.println("换购活动需要50元,谢谢你的参加"); }else if(sMoney>=50){ System.out.println("1 满50元, 加2瓶可乐"); System.out.println("2 满100元 ,加3瓶酸奶"); System.out.println("3 满100元,有10元面粉"); System.out.println("4 满200元,30元的喝喝"); System.out.println("5 满100元, 加5本子"); System.out.println("6 满300元, 加15个本子算20元"); System.out.println("0 不换"); System.out.println("请输入你的序号"); int a =sc.nextInt(); if(a>5 ||a<0) { System.out.print("你输入有误!如有需要轻重开始"); }else { if(a==6 && sMoney>=200) { System.out.println("满300元, 加15本子"); System.out.println("本次消费总额为:"+(sMoney+40)); }else if(a==5 && sMoney>=100) { System.out.println(" 满100元,90元的喝喝"); System.out.println("本次消费总额为:"+(sMoney+45)); }else if(a==4 && sMoney>=200) { System.out.println(" 满200元,30元的喝喝"); System.out.println("本次消费总额为:"+(sMoney+30)); }else if(a==3 && sMoney>=100) { System.out.println(" 满100元,有10元面粉"); System.out.println("本次消费总额为:"+(sMoney+10)); }else if(a==2 && sMoney>=100) { System.out.println(" 满100元 ,加3瓶酸奶"); System.out.println("本次消费总额为:"+(sMoney+19)); }else if(a==1 && sMoney>=50) { System.out.println(" 满50元, 加2瓶可乐"); System.out.println("本次消费总额为:"+(sMoney+4)); }else if(a==0 ) { System.out.println("欢迎光临!!!"); }else { System.out.println("你没有达到相应金额"); } } } } }
package Demo3; /** *第四章的第二题 *2022/3/9 */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class Test1 { /** * * @param args */ int[][] nums=null; void init() { System.out.println("输入数组的阶数:"); Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.close(); nums=new int[n][n]; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //随机性产生0 nums[i][j]=Math.random()>0.1?new Random().nextInt(9)+1:0; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { System.out.println("结果为"); } public static void main(String[] args) { new Test1().init(); } }
package Demo3; import java.util.Random; import java.util.Scanner; /** * 打印二维数组 * 求累加的和 * @author MZFAITHDREAM * */ public class Test2 { //创建二位数组 int[][] nums=null; private void init() { System.out.println("输入数组的阶数:"); //创建Scanner对象 Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.close(); nums=new int[n][n]; //循坏遍历数组打印二维数组 for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //随机性产生0的个数 nums[i][j]=Math.random()>0.1?new Random().nextInt(9)+1:0; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { //利用二位数组遍历累加 int sum=0; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { sum+=nums[i][j]; } } System.out.println("累加的结果为"+sum); } public static void main(String[] args) { //创建随机数组的阶数 new Test2().init(); } }
package Demo3; /** * 字符串专题一 * @author MZFAITHDREAM * */ public class Test3 { public static void main(String[] args) { String str1 = "I ME TO SE you"; int l = str1.length(); System.out.println("字符长度为"+str1.length()); System.out.println("====================================1============================"); /* * char charAt(int index)返回字符串指定索引的字符 * 注意:字符串中的第一个字符的索引为0 */ String str2 = "BGHYURET"; char index = str2.charAt(5); System.out.println(index); System.out.println("====================================2============================"); /* * String charAt(String str)将指定的字符串连接到该字符串的末尾 字符串的拼接 */ String str3 = "加油!"; String str4 = str3.concat("奥利给"); System.out.println(str4); System.out.println("====================================3============================"); /* * boolean endsWith(String str)测试此字符串是否以指定的后缀结尾。 */ String str5 = "hello.java"; boolean boo1 = str5.endsWith(".java"); System.out.println(boo1); System.out.println("====================================4============================"); /* * int indexOf(String str)返回指定子字符串第一次出现的字符串内的索引 */ String str6 = "abcdefehcd"; int index1 = str6.indexOf("acd"); System.out.println(index1); System.out.println("====================================5============================"); /* * int lastIndexOf(String str)返回指定子字符串最后一次出现的字符串内的索引 */ String str7 = "abcdefehcd"; int index2 = str7.lastIndexOf("cd"); System.out.println(index2); System.out.println("====================================6============================"); /* * String replace(char oldChar,char newChar)用新的字符将字符串的某些旧字符替换掉 */ String str8 ="XXX,我真的想骂你"; String str9 = str8.replace('X', '*'); System.out.println(str9); System.out.println("====================================7============================"); /* * String toLowerCase();将字符串中的所有大写字母转换为对应小写字母 */ String str10 = "ABC你好y"; String str11 = str10.toLowerCase(); System.out.println(str11); System.out.println("====================================8============================"); /* * String toUpperCase();将字符串中的所有小写字母转换为对应大写字母 */ String str12 = "ABC你好y"; String str13 = str12.toUpperCase(); System.out.println(str13); System.out.println("====================================9============================"); /* * String trim();去除字符串中首尾的空格 */ String str14 = " 1 23 "; String str15 = str14.trim(); System.out.println(str15); System.out.println("====================================10============================"); } }
package Demo3; /* * 定义一个方法,把 int 数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法, 并在控制台输出结果。 * 例如,数组为 int[] arr = {1,2,3}; ,执行方法后的输出结果为:[1, 2, 3] */ public class Test4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] arr={11,11,23,4,3678,245,34,3,67,89,345}; //"[1,2,3]" //调用方法,返回拼接好的字符串 String str = arrayToString(arr); System.out.println(str); } /** * 将一个数组转换为一个指令格式的字符串 */ public static String arrayToString(int[] arr){ //在方法中遍历数组,按照要求进行拼接 String s = ""; s += "["; //遍历数组 for(int i=0;i<arr.length;++i){ s=s+arr[i]; if(i==arr.length-1){ continue; } s=s+","; } s += "]"; return s; } }
package Demo3; import java.util.Scanner; public class Test5 { /** * 判断是不是二的整数方次 * @param args */ public static void main(String[] args) { //随机输入一个数组判断是不是二的整数方次 System.out.println("请输入你的数值"); Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); System.out.println(n); if((n&(n-1))==0) { System.out.println("是二的整数方"); }else { System.out.println("该数不是二的整数方"); } } }
package Demo3; /** *第四章的第二题 *2022/3/9 */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class Test6 { /** * * @param args */ int[][] nums=null; private void init() { System.out.println("输入数组的阶数:"); Scanner scanner =new Scanner(System.in); int n=scanner.nextInt(); //创结二维数组 nums =new int[n][n]; int num=0; for (int i = 0; i <nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { nums[i][j]=++num; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { //定义变量 int r=0,m=nums.length; int c=0,n=nums.length; //从left 到right boolean e=true; while(r<m&&c<n) { //从left下 到 右上的/ if(e) { System.out.println(nums[r][c]+" "); //第一行的边界 只能right if(r==0&&c<n-1) { e=!e; c++; continue; } else if(r>0&&c==n-1) { e=!e; continue; }else { r--; c++; } }else { //下 System.out.println(nums[r][c]+" "); if(c==0&& r==m-1) { e=!e; c++; continue; }else { r++; c--; } } System.out.println(c); } } public static void main(String[] args) { new Test1().init(); } }