2020.7.9
一、今日学习内容
1、对第二章的综合案例进行了验证
(1)扑克牌
1 public class CardDemo { 2 3 public static void main(String[] args) { 4 String[] suit= {"梅花","方块","红桃","黑桃"}; 5 String[] number= {"A","2","3","4","5","6","7","8","9","10","J","Q","k"}; 6 String[] cards=new String[64]; 7 for(int i=0;i<cards.length-2;i++) { 8 cards[i]=suit[i%suit.length]+number[i%number.length]; 9 } 10 cards[cards.length-2]="大王"; 11 cards[cards.length-1]="小王"; 12 for(int i=0;i<cards.length;i++) { 13 System.out.print(cards[i]+"\t"); 14 if(i%13==0&&i!=0)System.out.println(); 15 } 16 } 17 18 }
(2)ATM机取钱
1 import java.util.Scanner; 2 public class ATM { 3 public static void main(String[] args) { 4 System.out.print("请输入密码:"); 5 Scanner con=new Scanner(System.in); 6 String line=con.nextLine(); 7 if(line.equals("1234")) { 8 System.out.println("1、取款"); 9 System.out.println("2、退出"); 10 int num=con.nextInt(); 11 switch(num) { 12 case 1: 13 System.out.println("1、取款100"); 14 System.out.println("2、取款200"); 15 System.out.println("3、取款500"); 16 System.out.println("4、其他"); 17 int money=con.nextInt(); 18 switch(money) { 19 case 1: 20 System.out.println("取款100!"); 21 break; 22 case 2: 23 System.out.println("取款200!"); 24 break; 25 case 3: 26 System.out.println("取款500!"); 27 break; 28 case 4: 29 System.out.println("请输入取款金额:"); 30 int other=con.nextInt(); 31 System.out.println("取款"+other+"元!"); 32 break; 33 } 34 break; 35 case 2: 36 System.out.println("欢迎下次光临!谢谢!"); 37 break; 38 39 } 40 } 41 42 } 43 44 }
2、学习了第三章的内容值的比较
(1)值得比较
最大值:
1 public class MaxDemo { 2 public static void main(String[] args) { 3 int a=30; 4 int b=50; 5 int max=0; 6 if(a>b)max=a; 7 else max=b; 8 System.out.println("max="+max); 9 } 10 11 }
输出结果:max=50
1 public class MaxMoreDemo1 { 2 3 public static void main(String[] args) { 4 int[] a= {2,3,4,5,9,7,8}; 5 int max=0; 6 for(int i=0;i<a.length;i++) { 7 if(a[i]>max)max=a[i]; 8 } 9 System.out.println("max="+max); 10 } 11 12 }
1 public class MaxMoreDemo2 { 2 3 public static void main(String[] args) { 4 int[] a= {2,3,4,5,9,7,8}; 5 int max=0; 6 for(int i=0;i<a.length-1;i++) { 7 if(a[i]>a[i+1]&&a[i]>max) max=a[i]; 8 else if(a[i+1]>a[i]&&a[i+1]>max) max=a[i+1]; 9 } 10 System.out.println("max="+max); 11 } 12 13 }
1 public class MaxMoreDemo { 2 3 public static void main(String[] args) { 4 int[] a= {2,3,4,5,9,7,8}; 5 int max=0; 6 for(int i=1;i<a.length;i++) { 7 for(int j=i-1;j>=0;j--) { 8 if(a[j+1]>a[j]&&a[j+1]>max) max=a[j+1]; 9 } 10 } 11 System.out.println("max="+max); 12 } 13 14 }
以上三种方法均可找出最大值,输出结果为:max=9,最小值方法相同
平均值:
1 public class Avg { 2 3 public static void main(String[] args) { 4 int[] a= {2,3,4,5,9,7,8}; 5 int sum=0; 6 int avg=0; 7 for(int i=0;i<a.length;i++) { 8 sum+=a[i]; 9 } 10 avg=sum/a.length; 11 System.out.println("avg="+avg); 12 } 13 }
输出结果:avg=5
二、今日遇到的问题
今日的小学期题目还没有弄懂
三、明日计划
继续学习第三章数字的排序、查找,继续阅读大道至简