zoukankan      html  css  js  c++  java
  • Java 【小项目

    一.猜拳游戏

    import java.util.Scanner;
    
    public class demo{
        public static void main(String[] args){
          
           Scanner data = new Scanner(System.in);
           
           System.out.println("=====猜拳游戏====");
           System.out.println("请出:1.石头 2.剪子 3.布");
    
           int person = data.nextInt();
    
           int computer = (int)(Math.random()*3)+1;  //大于等于1
    
           String Marks = "拳头";
    
           String MarksC = "拳头";
    
           switch(person){
                case 1:
                Marks = "石头";
                break;
                case 2:
                Marks = "剪刀";
                break;
                case 3:
                Marks = "";
                break;
    
           }
             switch(computer){
                case 1:
                MarksC = "石头";
                break;
                case 2:
                MarksC = "剪刀";
                break;
                case 3:
                MarksC = "";
                break;
    
           }
    
           if(person == computer){
                     System.out.println("平局!"  + "
    电脑是:" + MarksC + "
    ");
                     System.out.print("你的是:" + Marks);
           }
           else if(person == 1 && computer == 3 || person == 2 && computer == 1 || person == 3 && computer == 2){
                    System.out.println("你输了!" + "
    电脑是:" + MarksC + "
    ");
                    System.out.print("你的是:" + Marks);        
     
           }
           else{
                 System.out.println("你赢了!" + "
    电脑是:" + MarksC + "
    ");
                 System.out.print("你的是:" + Marks);
           }
    
           
            
            }
        }
     

    二.随机数和输入输出

    import java.util.Scanner;
    import java.util.Random;
    
    public class demo{
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            Random  number = new Random();
            String name;
    
            int sum = 0;
            int flag = 0;
            int avg = 0;
            int value,A;
            System.out.println("请输入:");
            value = input.nextInt();
            do{
              //随机数产生
              A = number.nextInt(101);
    
              //计算求和
              sum += A;
              //计算平均数
             flag++;
    
            }while(flag<value);
            avg = sum/value;
            System.out.println("随机数的总和:" + sum);
            System.out.println("随机数的平均数:" + avg);
    
    
        } 
    }

     

    三.九九乘法表

    public class demo{
        public static void main(String[] args){
    
            for(int i=1;i<=9;i++){  //控制行
                for(int j=1;j<=i;j++){  //控制每行的个数,即列
    
                    System.out.print(j + "*" + i + "=" + j*i + "	");  //print()不能自动换行
                
                    
                }
                System.out.println();  //println()函数有换行功能
            }
        }
    }

    四.计算1997-2020年总天数

    public class demo{
        public static void main(String[] args){
            int sum =0;
            //输出1997-2020年的总天数
            for(int year=1997;year<2020;year++){
                //判断闰年和平年
                //闰年能被4或者400整除法
                if(year%400 == 0 || year%4 == 0 && year%100 != 0){
                    sum += 366;
                }
                else{
                    sum+=365;
                }
    
            }
            System.out.println("1997-2020年一共:" + sum + "");
        }
    }

    五.输出年份对应的12月

    import java.util.*;
    public class demo1{
        public static void main(String[] args){
            int sum =0;
            int year;
            Scanner input = new Scanner(System.in);
            
            System.out.println("输入年份:");
            year = input.nextInt();   //获取数值年
     
           //输出月的天数
            for(int month=1;month<=12;month++){
                if(month == 2){
                              if(year%400 == 0 || year%4 == 0 && year%100 != 0){
                                    System.out.println("29天");
                              }
                              else{
                                  System.out.println("28天");    
                              }
                }
                else{ 
                            if(month==4 || month == 6 || month ==9 || month ==11){
                                System.out.println("30天");
                            }
                            else{
                                System.out.println("31天");
                            }
              
                    }
            }
    
        }    
    }

    六.输出当日的星期数

    import java.util.*;
    
    //输出某天是星期几
    public class demo1{
        public static void main(String[] args){
    
            int value;
            int sum = 0;
            int year = 2012;
            //输出1900-2012年的总天数
            for(int years=1900;years<2012;years++){
                //判断闰年和平年
                //闰年能被4或者400整除法
                if(year%400 == 0 || year%4 == 0 && year%100 != 0){
                    sum += 366;
                }
                else{
                    sum+=365;
                }
    
            }
     
           //输出月的天数
            for(int month=1;month<=8;month++){
                if(month == 2){//判断二月两种
                              if(year%400 == 0 || year%4 == 0 && year%100 != 0){
                                    sum+=29;
                              }
                              else{
                                 sum+=28;    
                              }
                }
                else{ //判断其他
                            if(month==4 || month == 6 || month ==9 || month ==11){
                                sum+=30;
                            }
                            else{
                                sum+=31;
                            }
              
                    }
            }
               //9月1日当天
            //1900年1月1日-2012年9月1日总天数对7取模,值为星期数
            value = sum % 7;
            System.out.println("2012年9月1日是星期:" + value);  
    
        }    
    }

    七.显示某月的日历

    import java.util.*;
    
    //输出某天是星期几
    public class demo{
        public static void main(String[] args){
            //=============变量储存地方
            int value;  
            int sum = 0;
            int year = 2012;
    
            //========================计算总天数==============
            //输出1900-2012年的总天数
            for(int years=1900;years<2012;years++){
                //判断闰年和平年
                //闰年能被4或者400整除法
                if(year%400 == 0 || year%4 == 0 && year%100 != 0){
                    sum += 366;
                }
                else{
                    sum+=365;
                }
    
            }
     
           //截止到9月1日的天数
            for(int month=1;month<=8;month++){
                if(month == 2){//判断二月两种情况
                              if(year%400 == 0 || year%4 == 0 && year%100 != 0){
                                    sum+=29;
                              }
                              else{
                                 sum+=28;    
                              }
                }
                else{ 
                            if(month==4 || month == 6 || month ==9 || month ==11){
                                sum+=30;
                            }
                            else{
                                sum+=31;
                            }
              
                    }
            }
            //======================输出日历的部分=========
            int weekday = sum % 7;  //1900-2012.9.1的总天数除以7为星期数
            System.out.println("日	一	二	三	四	五	六");  //样式
            for(int i=1;i<=weekday;i++){  //首先日期的第一天应该空格的数量
                System.out.print("	");
            }
            for(int i= 1;i<=30;i++){  //当月的天的数值
                if(sum%7 == 6){   //如果是星期六
                    System.out.print(i + "
    ");  //输入i之后换行
                }
                else{
                    System.out.print(i + "	"); //否则输入i空格
                }
                sum++;  //总天数加1
            }
    
        }    
    }

    八.输入年月,输出日历

    import java.util.Scanner;
    //输出某天是星期几
    public class demo{
        public static void main(String[] args){
            //=============变量储存地方
            Scanner input = new Scanner(System.in);
            System.out.print("请输入年份:");
            int year = input.nextInt();
            System.out.print("请输入月份:");
            int month = input.nextInt();
            int sum = 0;
            //========================计算总天数==============
            //输出1900-2012年的总天数
            for(int i=1900;i<year;i++){
                //判断闰年和平年
                //闰年能被4或者400整除法
                if(i%4==0&&i%100!=0||i%400==0){
                    sum += 366;
                }
                else{
                    sum+=365;
                }
    
            }
     
           //截止到9月1日的天数
            for(int i=1;i<month;i++){
                if(i==2){//判断二月两种情况
                              if(year%400==0||year%4==0&&year%100!=0){
                                    sum+=29;
                              }
                              else{
                                 sum+=28;    
                              }
                        }
                else{ 
                            if(i==4||i==6||i==9||i==11){
                                sum+=30;
                            }
                            else{
                                sum+=31;
                            }
              
                    }
            }
            sum+=1;
            System.out.println("总天数:" + sum);
            //======================输出日历的部分=========
            int weekday = sum % 7;  //1900-2012.9.1的总天数除以7为星期数
            System.out.println("日	一	二	三	四	五	六");  //样式
            for(int i=1;i<=weekday;i++){  //首先日期的第一天应该空格的数量
                System.out.print("	");
            }
            for(int i=1;i<=30;i++){  //当月的天的数值
                if(sum%7 == 6){   //如果是星期六
                    System.out.print(i + "
    ");  //输入i之后换行
                }
                else{
                    System.out.print(i + "	"); //否则输入i空格
                }
                sum++;  //总天数加1
            }
    
        }    
    }
    View Code

    九.日历的美中不足补充

    从上面可以看到,一月份输出30天,因没有判断,所以说增加了判断

    import java.util.Scanner;
    //输出某天是星期几
    public class demo{
        public static void main(String[] args){
            //=============变量储存地方
            Scanner input = new Scanner(System.in);
            System.out.print("请输入年份:");
            int year = input.nextInt();
            System.out.print("请输入月份:");
            int month = input.nextInt();
            int sum = 0;
            //========================计算总天数==============
            //输出1900-2012年的总天数
            for(int i=1900;i<year;i++){
                //判断闰年和平年
                //闰年能被4或者400整除法
                if(i%4==0&&i%100!=0||i%400==0){
                    sum += 366;
                }
                else{
                    sum+=365;
                }
    
            }
     
           //截止到9月1日的天数
            for(int i=1;i<month;i++){
                if(i==2){//判断二月两种情况
                              if(year%400==0||year%4==0&&year%100!=0){
                                    sum+=29;
                              }
                              else{
                                 sum+=28;    
                              }
                        }
                else{ 
                            if(i==4||i==6||i==9||i==11){
                                sum+=30;
                            }
                            else{
                                sum+=31;
                            }
              
                    }
            }
            sum+=1;
            System.out.println("总天数:" + sum);
            //======================输出日历的部分=========
            int weekday = sum % 7;  //1900-2012.9.1的总天数除以7为星期数
            System.out.println("日	一	二	三	四	五	六");  //样式
            for(int i=1;i<=weekday;i++){  //首先日期的第一天应该空格的数量
                System.out.print("	");
            }
            //判断是否是闰年和平年,闰年2月29,平年2月28
            int month_value = 0;  //判断显示月份的天数
            if(month==2){//判断二月两种情况
                              if(year%400==0||year%4==0&&year%100!=0){
                                    month_value=29;
                              }
                              else{
                                 month_value=28;    
                              }
                        }
                else{ 
                            if(month==4||month==6||month==9||month==11){
                                month_value=30;
                            }
                            else{
                                month_value=31;
                            }
              
                    }
            for(int i=1;i<=month_value;i++){  //当月的天的数值
                if(sum%7 == 6){   //如果是星期六
                    System.out.print(i + "
    ");  //输入i之后换行
                }
                else{
                    System.out.print(i + "	"); //否则输入i空格
                }
    
                sum++;  //在for循环中,每输出一个增加到最后
            }
    
        }    
    }
    View Code
  • 相关阅读:
    WCF全面解析学习之地址Address(1)
    【转】理解和使用Oracle 8i分析工具-LogMiner
    【Wonder原创】NHibernate映射文件范例
    【转】C#经典面试题及答案
    【转】C#中abstract class和interface之探讨
    【转】130道C#面试题
    【Oracle学习】archivelog
    【转载】sql server 2005系统表详细说明
    【转】WINDOWS批处理命令详解
    【转】Linux find命令详解
  • 原文地址:https://www.cnblogs.com/Crown-V/p/12401110.html
Copyright © 2011-2022 走看看