• JAVA基础语法


    1.Java数据类型分几大类?,基本类型有哪些?
    基本数据类型,引用数据类型
    基本类型:数值型,字符型,布尔型
    2.基本数据类型转换规则是什么?
    隐式转换,显示转换。
    boolean不能和其他类型转换。
    有多种类型的数据混合运算时,系统首先自动将所有数据转换成容量最大的那一种类型,然后计算。
    3.常用的运算符有哪几大类,分别是什么?
    算术运算符,比较运算符,逻辑运算符
    4.“+” 运算符有哪几种用法,需要注意什么?
    ①数值+数值②字符串连接 需要注意加法运算时数值类型转换 ±可隐式转换数据类型
    5.“++”运算符在使用时需要注意什么?
    前缀后缀形式不同进行自增的时间不同
    6.& 与 && 的区别?
    &:如果表达式第一个为false,后面的表达式还会执行。
    &&: 如果表达式第一个为false,后面的表达式不会执行。

    1.判断某一年是否为闰年
    	通过Scanner 输入一个年份,然后判断概念是否是闰年
    	闰年判断标准(满足任何一个)
    	1.如果能够被4整除&&但是不能被100整除
    	2. 能够被400整除
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public class _1 {
        public static void main(String[] args) {
            Scanner cin=new Scanner(System.in);
            int year;
            year=cin.nextInt();
            if((year%4==0&&year%100!=0)||(year%400==0))
                System.out.println("yes");
            else System.out.println("no");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    2.根据指定月份,打印该月份所属的季节,3,4,5春季  678夏季  9 10 11秋季  12 ,1,2 冬季
    
    • 1
    public class _2 {
        public static void main(String[] args) {
            Scanner cin=new Scanner(System.in);
            byte month;
            while(cin.hasNextByte())
            {
                month=cin.nextByte();
                if(month<=2&&month>0||month==12) System.out.println("冬");
                else if(month>=3&&month<=5) System.out.println("春");
                else if(month>=6&&month<=8) System.out.println("夏");
                else if(month>=9&&month<=11)System.out.println("秋");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    3.我家的狗5岁了,5岁的狗相当于人的多大呢?,其实狗的前两年,每一年相当于人的10.5岁,之后每增加一岁就增加4岁,
    那么5岁的狗相当于人的年龄就应该是10.5+10.5+4+4+4=33岁.
    编写程序获取用户输入狗的年龄(整数),通过程序输出显示相当于人的年龄.如果为负数请提示.
    两种情况
    
    • 1
    • 2
    • 3
    • 4
    package Demo1;
    
    import java.util.Scanner;
    
    public class _3 {
        public static void main(String[] args) {
            Scanner cin=new Scanner(System.in);
            int age=cin.nextInt();
            if(age<0) System.out.println("请输入正确数值!");
            if(age<=2) System.out.println(age*10.5);
            else System.out.println(21+(age-2)*4);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    4.一直青蛙在一口11米深的井底向上爬,白天向上爬3米,晚上向下滑2米,总共需要几天可以爬出. 
      写程序实现.
    
    • 1
    • 2
    package Demo1;
    
    import java.util.Scanner;
    
    public class _4 {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            int dept=cin.nextInt();
            int days=0;
            while(dept>0)
            {
                if(dept>3)dept-=3;
                else
                {
                    dept=0;
                    days++;
                    break;
                }
                dept+=2;
                days++;
            }
            System.out.println(days);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    5.输出奇数100以内最大的5个奇数
    
    • 1
    package Demo1;
    import java.util.Scanner;
    public class _5 {
        public static void main(String[] args) {
            Scanner cin=new Scanner(System.in);
            for(int i=100,j=0;i>0;i--)
            {
                if(i%2!=0&&j<5)
                {
                    System.out.println(i);
                    j++;
                }
                if(j==5)break;
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    6.有1、2、3、4四个数字,能组成多少个互不相同且一个数字中无重复数字的三位数?
    
    • 1
    package Demo1;
    
    public class _6 {
        public static void main(String[] args) {
            for(int i=1;i<=4;i++)
            {
                for(int j=1;j<=4;j++)
                {
                    for(int k=1;k<=4;k++)
                    {
                        if(i!=j&&i!=k&&j!=k) System.out.println(""+i+j+k);
                    }
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    7.编程一个程序,计算今天是今年的第几天.
    
    • 1
    package Demo1;
    import java.util.Scanner;
    public class _7 {
        public static void main(String[] args) {
            Scanner cin=new Scanner(System.in);
            int year,month,day,days=0;
            year=cin.nextInt();
            month=cin.nextInt();
            day=cin.nextInt();
            boolean flag;
            if(year%4==0&&year%100!=0||(year%400==0))flag=true;
            else flag=false;
            switch(month-1)
            {
                case 12:days+=31;
                case 11:days+=30;
                case 10:days+=31;
                case 9:days+=30;
                case 8:days+=31;
                case 7:days+=31;
                case 6:days+=30;
                case 5:days+=31;
                case 4:days+=30;
                case 3:days+=31;
                case 2:
                {
                    if(flag==false)days+=29;
                    else days+=28;
                }
                case 1:days+=31;
            }
            days+=day;
            System.out.println(days);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    8.猴子吃桃问题。猴子第一天摘下若干个桃子,当时就吃了一半,还不过瘾,
    就又吃了一个。第二天又将剩下的桃子吃掉一半,又多吃了一个。以后每天都吃
    前一天剩下的一半零一个。到第 10 天在想吃的时候就剩一个桃子了,求第一天共
    摘下来多少个桃子?  
    
    • 1
    • 2
    • 3
    • 4
    package Demo1;
    
    import java.util.Scanner;
    
    public class _8 {
        public static void main(String[] args) {
            Scanner cin=new Scanner(System.in);
            int sum=1;
            for(int i=0;i<10;i++)
            {
                sum=(sum+1)*2;
            }
            System.out.println(sum);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    9.水仙花数是指一个 3 位数,它的每个位上的数字的3次幂之和等于它本身。
     (例如:1^3 + 5^3 + 3^3 = 153)。编程求出1-1000以内的水仙花数。
    
    • 1
    • 2
    package Demo1;
    
    public class _9 {
        public static void main(String[] args) {
            for(int i=1;i<=1000;i++)
            {
                int a=i%10;
                int b=i/10%10;
                int c=i/100%10;
                if(i==a*a*a+b*b*b+c*c*c) System.out.println(i);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    10.编写程序解决“百钱买百鸡”问题。
      公鸡五钱一只,母鸡三钱一只,
      小鸡 一钱三只,
      现有百钱欲买百鸡,各买鸡多少只?
        100块 买100只鸡
    	公鸡,母鸡,小鸡都有
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
       11.输出2-100以内所有的质数.
      
      • 1
      package Demo1;
      
      public class _10 {
          public static void main(String[] args) {
              for(int i=2;i<=100;i++)
              {
                  if(prime(i)==true) System.out.println(i);
              }
          }
          public static boolean prime(int x)
          {
              for(int i=2;i<=x/i;i++)
              {
                  if(x%i==0)return false;
              }
              return true;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    • 相关阅读:
      Roson的Qt之旅#100 QML四种标准对话框(颜色、字体、文件、提升)
      【工具推荐】替换typora的又一款神器
      ffmpeg安装及使用
      【kubernetes篇】使用Harbor仓库管理kubernetes镜像
      【GBASE培训】GBase数据库2022年第6期培训圆满结束
      【FLASH存储器系列五】SPI NOR FLASH芯片使用指导之一
      代码随想录算法训练营 动态规划part16
      C#截取某一范围的图
      Spring AOP:面向切面编程
      10_JavaWeb过滤器
    • 原文地址:https://blog.csdn.net/m0_71385141/article/details/132745165