学习java第四天写的小程序

Day04课后小程序

 

1、1-100之间偶数和

2、1-100之间奇数和

3、1-100之间既能被3整除又能被5整除的(且要求跳过能够被10整除的数)所有数之和。

以上1至3题三题代码如下:

代码:

/**

*作用:计算1-100之间各个数的和

*作者:李林雄

*

*/

public class Test2Even{

         public static void main(String[] args){

                   //定义三个变量分别用来计算各个数相加的合

                   int sum=0;

                   int sum1=0;

                   int sum2=0;

                   for(int i=1;i<=100;i++){

                            if(i%2 == 0){

                                     sum = sum+i;

                            }

                            if(i%2 != 0){

                                     sum1 = sum1+i;

                            }

                           

                            if(i%15 == 0 && i%10 != 0){

                                     sum2 = sum2+i;

                            }

                   }

                   System.out.println("1-100之间偶数之和为"+sum);

                   for(int i=1;i<=30;i++){

                            System.out.print("*");

                   }

                   System.out.println();

                   System.out.println("1-100之间奇数之和为"+sum1);

                  

                   for(int i=1;i<=30;i++){

                            System.out.print("*");

                   }

                   System.out.println();

                   System.out.println("1-100之间能够同时被3和5整除并且不能够被10整除的数之和为"+sum2);

         }

        

}

输入结果如下:

 

 

 

 

4、100以内前6个3的倍数的数字之和

带入如下:

代码如下:

/**

*作用:求出100以内前6个3的倍数的数字之和

*作者:李林雄

*

*/

public class Test3EvenWhile{

         public static void main(String[] args){

                   //定义一个变量用来存储各个数之和

                   int sum=0;

                   int a=0;

                   for(int i=1;i<=100;i++){

                           

                            if(i%3 == 0){

                                     sum = sum+i;

                                     a++;

                            }

                            //如果a=6那么说明i内的语句已经执行了6次说明已经加到了第六个所以不再执行

                            if(a == 6){

                                     break;

                            }

                   }

                   System.out.println("1-100前六个能被3整数的数之和为:"+sum);

         }

}

输出结果如下:

 

 

 

 

5、取款机设计

代码如下:

/**

*作用:编写一个取款机的密码输入程序

*作者:李林雄

*/

import java.util.Scanner;

public class Test4GetMoney{

         public static void main(String[] args){

                   Scanner input = new Scanner(System.in);

                   int num = 3;

                   do{

                            System.out.print("请输入您的密码:");

                            int password = input.nextInt();

                            if(password == 123456){

                                     System.out.println("密码输入正确,请随便取款");

                                     break;

                            }

                            num--;

                            if(num == 0){

                                     System.out.println("您的账户已被冻结,请拿着身份证到银行解除冻结");

                                     break;

                            }

                                    

                            System.out.println("您的密码输入错误您还有"+num+"次输入的机会");

                            System.out.println("请还要继续输入吗,如果继续请按y,按其他任意键将会退出");

                            String  ch = input.next();

                            if(ch.equals("y")){

                                     continue;

                            }else{

                                     System.out.println("感谢您的使用,欢迎下次光临");

                                     break;

                            }

                   }while(num > 0);

         }

}

输入结果如下:

 

 

 

6、要求循环录入2个班级的学员成绩,假设每个班都有3个学员,依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。

代码如下:

/**

*作用:录入两个班级的分数,并求出平均值

*作者:李林雄

*/

import java.util.Scanner;

public class Test5AverageScore{

         public static void main(String[] args){

                   Scanner input = new Scanner(System.in);

                   int num = 0;

                   double sum = 0;

                   for(int i = 1;i<=2;i++){

                            for(int j=1;j<=3;j++){

                                     System.out.println("请输入第"+i+"个班的第"+j+"个学生的成绩0-100之间的数字");

                                     int score = input.nextInt();

                                     if(score >= 0 && score <=100){

                                               if(score > 90){

                                               num++;

                                               sum = sum + score;

                                               }

                                     }else{

                                               System.out.println("输入的成绩有误请重新输入!!!");

                                               j--;

                                               continue;

                                     }

                            }

                   }

                   if(num == 0){

                            System.out.println("超过90分的人数有"+num+"个");

                   }else{

                            System.out.println("超过90分的人数有"+num+"个");

                            System.out.println("超过90分这批学员的平均分为:"+(sum/num));

                   }

         }

}输出结果:

 

 

7、设计一个剪刀石头布的小程序:

代码如下:

/**

*作用:制作一个石头剪刀布的游戏

*作者:李林雄

*/

import java.util.Scanner;

public class Test6Game{

         public static void main(String[] args){

                   Scanner input = new Scanner(System.in);

                   int GameNum = 0;

                   int LuckNum = 0;

                   int count = 0;//用于计算玩的总数

                   int win = 0;//用于计算赢的次数

                   int lose = 0;//用于计算输的次数

                   int tie = 0;//用于计算打平的次数

                   int mistake = 0;//用于定义输入错误的次数

                   do{

                            count++;

                            GameNum = (int)(Math.random()*3)+1;

                            System.out.println("请输入你要输入的数字:1代表剪刀,2代表石头,3代表布");

                            LuckNum = input.nextInt();

                            if(LuckNum <=3 && LuckNum >=0){

                                     //计算跟系统达成平手的判断

                                     if(GameNum == LuckNum){

                                               tie++;

                                               if(GameNum == 1){

                                                        System.out.println("您跟系统的输入都为剪刀:"+GameNum+"所以打成平手,");

                                                        System.out.println("按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                               if(GameNum == 2){

                                                        System.out.println("您跟系统的输入都为石头:"+GameNum+"所以打成平手,按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                               if(GameNum == 3){

                                                        System.out.println("您跟系统的输入都为布:"+GameNum+"所以打成平手,按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                     }else{//否则跟系统不能打成平手的时候

                                               //当赢系统的时候总共有三次赢系统的情况

                                               if(LuckNum == 2 && GameNum == 1){

                                                        win++;

                                                        System.out.println("您输入的是石头:"+LuckNum+"系统输入的是剪刀:"+GameNum+"所以您赢了");

                                                        System.out.println("按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                               if(LuckNum == 3 && GameNum == 2){

                                                        win++;

                                                        System.out.println("您输入的是布:"+LuckNum+"系统输入的是石头:"+GameNum+"所以您赢了");

                                                        System.out.println("按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                               if(LuckNum == 1 && GameNum == 3){

                                                        win++;

                                                        System.out.println("您输入的是剪刀:"+LuckNum+"系统输入的是布:"+GameNum+"所以您赢了");

                                                        System.out.println("按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                               //当输系统的时候总共有三次输系统的情况

                                               if(LuckNum == 1 && GameNum == 2){

                                                        lose++;

                                                        System.out.println("您输入的是剪刀:"+LuckNum+"系统输入的是石头:"+GameNum+"所以您输了");

                                                        System.out.println("按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                               if(LuckNum == 2 && GameNum == 3){

                                                        lose++;

                                                        System.out.println("您输入的是石头:"+LuckNum+"系统输入的是布:"+GameNum+"所以您输了");

                                                        System.out.println("按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                               if(LuckNum == 3 && GameNum == 1){

                                                        lose++;

                                                        System.out.println("您输入的是布:"+LuckNum+"系统输入的是剪刀:"+GameNum+"所以您输了");

                                                        System.out.println("按y再玩一次,任意键退出游戏");

                                                        String ch = input.next();

                                                        if(ch.equals("y")){

                                                                 continue;

                                                        }else{

                                                                 System.out.println("游戏结束");

                                                                 break;

                                                        }

                                               }

                                     }

                           

                            }else{

                                     mistake++;

                                     System.out.println("输入有误,请重新输入");

                                     continue;

                            }

                           

                   }while(true);

                  

                   System.out.println("游戏结束,欢迎下次再玩,本次游戏你总共跟系统玩了"+count+"次,其中赢了"+win+"次,输了"+lose+"次,平手"+tie+"次,其中输入错误的次数为:"+mistake+"次");

         }

}                

输出结果如下:

 

 

 

 

posted @ 2017-03-30 20:06  java李  阅读(2517)  评论(0编辑  收藏  举报