• 分支


    表达式
    变量或常量和运算符组成的式子,表达式
    Java运算符
    赋值运算符 =
    等号的左边只能是变量
    等号的右边可以使 变量,常量,表达式
    先计算右边,然后将得到的值赋值给左边
    int a = 10
    算数运算符
    + - * / %
    除数不能为0,数学中是没有意义
    表达中使用0作为除数,抛出异常(警告信息
    练习:
    已知两个变量a=7,b=3,求和,差,商,积,余数
    已知char变量值是’a’,通过某种计算得到A
    已知圆的半径是1.5,求面积和周长
    代码:
    public class ArithmeticOperator{

    public static void main(String[] args) {
    /*
    1.已知两个变量a=7,b=3,求和,差,商,积,余数
    2.已知char变量值是’a’,通过某种计算得到A
    3.已知圆的半径是1.5,求面积和周长
    */
    //1.
    //定义两个变量
    int a = 7;
    int b = 3;
    //需要两个变量进行计算,找出对应的运算符
    //a+b --> 7+3 --> a+b 变量和运算符组成的式子 --> 表达式 得到结果
    //如何拼接打印结果
    //+有两个作用: 在算数运算符中,是求和 若是两个字符串使用+ ,就是拼接的意思 " "引起来的内容就是字符串
    System.out.println("和:"+(a+b));
    System.out.println("差:"+(a-b));
    System.out.println("商:"+(a/b));
    System.out.println("乘积:"+(a*b));
    System.out.println("余数:"+(a%b));
    int sum = a+b;
    System.out.println("和:"+sum);
    System.out.println("--------------------------------华丽的分割线------------------------------");
    //2
    char ch = 'a';
    //a~z A-Z '0'-'9'
    //97 65 48
    //字符可以通过数值计算的到改变
    //97-65 = 32
    //98-66 = 32
    ch = (char)(ch-32);//小字母变大字母 // 大字母变小字母+32
    System.out.println(ch);
    System.out.println("--------------------------------华丽的分割线------------------------------");
    //3
    double r = 1.5;
    double PI = 3.14;
    //周长:2*PI*r
    System.out.println(2*PI*r);
    //面积:PI*r*r
    System.out.println(PI*r*r);
    /*
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at ArithmeticOperator.main(ArithmeticOperator.java:34)

    */
    // System.out.println(10/0);


    }
    }

    赋值运算符(复合运算符)
    += -= *= /= %=
    变量a = 7, b = 3
    a = a+b --> a+=b
    a+=b
    1. 将等号左边的a+移动到等号的右边,和b组成一个表达式,等号左边保留
    a+ = a+b;
    2. 等号的左边只能保留变量,所以等号左边的运算符需要去掉保留变量
    a = a+b;
    3. 先计算表达式右边的值,然后将值赋值给等号左边的变量
    a = 7+3 --> a =10

    ps:若运算符的右边是一个表达式
    a *= 2+3; --> a* = a*(2+3)--> a = a*(2+3) --> a = 7*(2+3) -->

    a =35

    关系运算符
    > 大于
    >= 大于等于 --> 大于 或 等于
    < 小于
    <= 小于等于 --> 小于 或 等于
    == 相等 只要值是相同的 就为真 否则为假
    != 不相等 只要不相等 就为真 否则为假
    若使用关系运算符组成的表达式值只有两种
    true 成立(真) false不成立(假)
    ps:boolean值只有true 和 false
    代码:
    public class GuanXiOperator{

    public static void main(String[] args) {
    int a = 7;
    int b = 3;
    boolean result = a > b; // 7 > 3 --> true
    System.out.println(result);//true
    System.out.println(a >= b);//true
    System.out.println(a < b);//false
    System.out.println(a <= b);//false
    System.out.println(a == b);//false
    System.out.println(a != b);//true不等于-->只要不相等就是真的 7 != 3


    }
    }

    逻辑运算符
    &与(并且) |或(或者)

    &&短路与(与) ||短路或(或)
    ! --> 取非 --> 取相反值
    !true -- > false 取当前的对立面结果
    public class GuanXiOperator{
    //与和或
    public static void main(String[] args) {

    //逻辑运算符是和表达式之间进行运算符
    //ps:表达式:关系表达式
    //表达式1 & 表达式2
    // 7即是大于3并且也大于4
    //boolean result = 7 > 3 & 7 > 4;
    // true & true

    //或的演示:
    //只要有一个表达式的值是真,整个表达式的值都是真
    //只有所有的表达式的值为假,整个表达式的值就是假
    System.out.println(true | true); //true
    System.out.println(false | true);//true
    System.out.println(true | false);//true
    System.out.println(false | false);//false
    System.out.println("---------------------------------华丽的分割线----------------------------"); //true

    //与(并且)
    //只有表达式都为真,整个表达式的值才能为真
    //只要有有一个为假,整个表达式的值都是假
    System.out.println(true & true); //true
    System.out.println(false & true);//false
    System.out.println(true & false);//false
    System.out.println(false & false);//false
    System.out.println("---------------------------------下面的结果是短路与和或----------------------------");


    //短路或的演示:
    //只要有一个表达式的值是真,整个表达式的值都是真
    //只有所有的表达式的值为假,整个表达式的值就是假
    System.out.println(true || true); //true
    System.out.println(false || true);//true
    System.out.println(true || false);//true
    System.out.println(false || false);//false
    System.out.println("---------------------------------华丽的分割线----------------------------");

    //短路与(并且)
    //只有表达式都为真,整个表达式的值才能为真
    //只要有有一个为假,整个表达式的值都是假
    System.out.println(true && true); //true
    System.out.println(false && true);//false
    System.out.println(true && false);//false
    System.out.println(false && false);//false

    //结论:在正常使用的情况下,与和或 同 短路与和短路或的结果是完全相同
    /*
    为什么开发中使用短路与和短路或
    使用与 和 短路与进行比较分析
    false & true 先计算表达式1的值得到结果之后会继续计算表达式2的值,当得到表达式2的值时,同时计算表达式1和表达式2共同值的结果,最终得到整个表达式的值
    false && true 先计算表达式1的值,若表达式1的值为真,才会计算表达式2的值,若表达式1的值为假,就直接认为整个表达式的值是假的
    */

    }
    }

    练习:
    1. 参加少年运动会,年龄在13~17之间才可以参加
    age =>13 && age <= 17
    2. 动物园年龄小于12的儿童,大于65的老人免票
    age < 12 || age > 65
    3. 年龄不小于18岁才可以观看
    age >= 18 --> !(age < 18)
    判断下列哪个是false哪个是true
    97 99
    (100>3) && (‘a’ > ‘c’) false
    (100>3) || (‘a’ > ‘c’) true
    !(100>3) false

    构造表达式:
    number等于或大于90但小于100
    number>=90 && number<=100
    ch不是字符q也不是字符k
    ch!=’q’&& ch!=k’
    number介于1到9之间(包括1但是不包括9),但是不等于5
    (number>=1 && number<9) && (number!=5)
    number不在1到9之间
    number<1 || number>9 --> !(number>=1 && number<=9)
    判断这个字符是空格,是数字,是字母
    有3个整数a,b,c,判断谁最大,列出所有可能性
    判断year是否是如年,符合下面二者之一即可
    能被4整除但是不能被100整除
    能被400整除
    整除除以整数只能得到整数
    (year%4==0 && year%100!=0 )||(year%400==0)

    ++ -- 运算符 循环中(不区分前++和后++)
    前++ : 在变量前出现++运算符 ,称为前++
    后++ : 在变量之后出现++运算符,称为后++
    -- 同理
    不同点:
    若是前++ ,先对变量进行自身+1操作,然后变量在参与运算
    若是后++,先对变量进行操作,然后在自身+1
    例子:
    int i = 1;
    i ++; -->i+=1--> i = i+1
    ps,无论如何操作只要是++运算符都要进行自身+1操作

    -- 同理 自身进行-1操作
    例子
    int i = 2;
    i -- ; --> i-=1--> i = i-1;
    //代码
    public class AddAndSubDemo {
    public static void main(String[] args) {
    int a = 1;
    int b = 1;
    int aplus;
    int bplus;
    aplus = a++; // aplus = a aplus = 1 a++ --> a= a+1 a = 1+1 a = 2
    bplus = ++b; // ++b --> b = b+1 b = 1+1 b = 2 bolus = b bplus = 2
    System.out.println(a);//2
    System.out.println(b);//2
    System.out.println(aplus);//1
    System.out.println(bplus);//2


    }
    }
    //代码
    public class AddAndSubDemo1 {
    public static void main(String[] args) {
    int x = 4;
    int y = 5;
    // 4 + 6
    boolean result = (x--)+(++y) <= 7;
    System.out.println(result);//false
    }
    }
    a=3,b=4
    (a++)/3+(--b)*2-(a--)%6+(b++)*3-(b--) = 8
    a: 4 3
    b: 3 4 3
    计算: 3/3+3*2-4%6+3*3-4 = 1+6-4+9-4 = 8


    a=3,b=4
    (++b)*2-(a--)%4+(a++)*5-(--b)/2+(--a) = 17
    a: 2 3 2
    b: 5 4
    计算: 5*2- 3%4+2*5-4/2+2 = 10-3+10-2+2 = 17

    a=3,b=4
    (a--)*6+(b++)/3-(--a)*2-(--b)*2+(++a)
    a: 2 1 2
    b: 5 4
    计算:3*6+4/3-1*2-4*2+2=18+1-2-8+2 = 11
    int x = 10;
    ps:java中是做不了这个 ++x++

    位运算符(了解)
    & 按位与
    | 按位或
    ^ 按位异或
    ~ 按位取反
    << 左位移
    >> 右位移
    >>> 无符号右位移
    代码:
    public class WeiDemo {
    public static void main(String[] args) {
    System.out.println(5 & 9);
    /*
    有1为1 有0为0 1和0 为 0
    0000 0101 5
    0000 1001 9
    --------------
    0000 0001 1
    */
    System.out.println(5 | 9);
    /*
    有1为1 有0为0 1和0 为 1
    0000 0101 5
    0000 1001 9
    --------------
    0000 1101 13
    */
    System.out.println(5 ^ 9);
    /*
    有1为0 有0为0 1和0 为 1
    0000 0101 5
    0000 1001 9
    --------------
    0000 1100 12
    */
    //一个数对另一个数异或两次,该数本身不变
    System.out.println(5 ^ 9 ^ 9);

    // 正数取反 负数,原有数值+1
    // 负数取反 正数,原有数值-1
    System.out.println(~5);
    /*
    0000 0101
    第一次取反
    1111 1010 -1
    1111 1001
    第二次取反,符号位不动其他位取反
    1 000 0110 -6
    */

    System.out.println(~(-5));
    /*
    1000 0101
    1111 1010 +1
    1111 1011
    进行取反,符号位要跟着改变
    0000 0100 4
    */
    //负数的符号位不动
    System.out.println(2<<2);
    // 0000 0010 0000 1000
    System.out.println(2>>2);
    }
    }

    分支:
    迄今为止,我们写的Java代码都是一条一条语句顺序执行,这种结构的代码我们称之为顺序结构。然而仅有顺序结构并不能解决所有的问题,比如我们设计一个游戏,游戏第一关的通关条件是玩家获得1000分,那么在完成本局游戏后我们要根据玩家得到分数来决定究竟是进入第二关还是告诉玩家“Game Over”,这里就会产生两个分支,而且这两个分支只有一个会被执行,这就是程序中分支结构。
    Java语言程序的结构 顺序,分支,循环
    顺序结构
    class A{
    public static void main(String[] agrs){
    int a = 1;
    int b = 2;
    int sum = a+b;
    System.out.println(sum);
    System.out.println(“第二关”);
    System.out.println(“Game Over”)

    }

    }
    分支:
    Java中的分支
    if分支
    if单分支 if-else if - else if if-else嵌套
    switch...case分支
    分支的构成
    1. 条件,可以进行判断(选择条件)
    一般是有关系和逻辑运算符组成的表达式

    2. if分支得到的判断结果只能是true 或 false
    3. 通过判断之后会得到对应的结果 --> 执行语句
    if单分支
    只有一次判断
    语法:
    if(表达式){
    执行语句;
    }
    表达式:一般是由关系和逻辑运算符组成
    值只有true 和 false
    执行语句: 打印,计算,再次调用, 书写if逻辑, 调用方法等等

    执行过程:当程序执行到if时,先判断表达式(判断条件)
    的真假,若为真,就会执行{ }里面的执行语句,执行语句结束后,if分支就会结束,若为假,那么久不会执行{}中的执行语句,而是继续向下执行
    代码
    //导入包
    import java.util.Scanner;
    public class IFDemo {
    public static void main(String[] args) {
    //在控制台上获取数据
    Scanner input = new Scanner(System.in);
    //获取整数 --> 回车作为结束
    //多个数据输入,可以多次使用回车输入 或是使用 空格隔开数值
    int fs = input.nextInt();
    if(fs >= 10000){
    System.out.println("可以进入第二关");
    }

    }
    }
    //导入包
    import java.util.Scanner;
    public class IFDemo {
    public static void main(String[] args) {
    /*
    //在控制台上获取数据
    Scanner input = new Scanner(System.in);
    //获取整数 --> 回车作为结束
    //多个数据输入,可以多次使用回车输入 或是使用 空格隔开数值
    int fs = input.nextInt();
    if(fs >= 10000){
    System.out.println("可以进入第二关");
    }
    */
    //练习:1.输入两个不等数,比较出谁最大,将其值打印出来
    System.out.println("请输入两个数:");
    Scanner input = new Scanner(System.in);
    int num1 = input.nextInt();
    int num2 = input.nextInt();
    //在使用if-else的情况下,使用if完成判断
    //两个值的交换
    /*
    数学的方式 --> 求和 只能计算数值
    int a = 1 ,b = 2
    int c = a+b;
    a = c-a;
    b = c-b;
    第三方变量
    int a = 1,b = 2
    int tmp = 0;
    tmp = a;
    a = b;
    b = tmp;
    笔试中,使用异或运算符
    int a =1 ,b = 2;
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    解释: 首先同一个数被异或两次可以得到原有值,所以根据这个理论
    a = a ^ b 可以的到一个新的值,然后 b = a ^ b,a的值已经改变了在此异或同一个b可以的到原来存a的值,那么b中存储的就是a的值
    然后在通过 a = a ^ b 相同异或就可以实现 a和 b 之间的互相交换
    */
    /*
    if(num1 > num2){
    System.out.println("最大值是:"+num1);
    }
    if(num1 < num2){
    System.out.println("最大值是:"+num2);
    }
    */
    //修改
    if (num1 < num2){
    int tmp = num1;
    num1 = num2;
    num2 = tmp;
    }
    System.out.println(num1);


    System.out.println("--------------------------------------------华丽的分割线--------------------------------------------------------");
    // 2.输入一个数,判断是偶数或奇数或3的倍数
    System.out.println("请输入一个数:");
    int num3 = input.nextInt();
    if(num3%2 == 0){
    System.out.println("偶数:"+num3);
    }
    if(num3%2 == 1){
    System.out.println("奇数:"+num3);
    }
    if(num3%3 == 0){
    System.out.println("3的倍数:"+num3);
    }

    System.out.println("--------------------------------------------华丽的分割线--------------------------------------------------------");
    //3.输入三个不等数,按照从小到大的顺序输出
    //假设,三个数是从大到小 3, 2, 1 --> 1,2,3

    int a = input.nextInt();
    int b = input.nextInt();
    int c = input.nextInt();
    //标准方式
    /*
    if(a>b && a>c && b>c){
    System.out.println(c+" "+b+" "+a);
    }

    if(a>b && a>c && c>b){
    System.out.println(b+" "+c+" "+a);
    }

    if(b>a && b>c && a>c){
    System.out.println(c+" "+a+" "+b);
    }

    if(b>a && b>c && c>a){
    System.out.println(a+" "+c+" "+b);
    }

    if(c>a && c>b && b>a){
    System.out.println(a+" "+b+" "+c);
    }

    if(c>a && c>b && a>b){
    System.out.println(b+" "+a+" "+c);
    }
    */
    //3,2,1 --> 1,2,3
    if (a > b){
    int tmp = a;
    a = b;
    b = tmp;
    }
    //2,3,1
    if(a > c){
    int tmp = a;
    a = c;
    c = tmp;
    }
    //1,3,2
    if(b > c){
    int tmp = b;
    b = c;
    c = tmp;
    }
    //1,2,3
    System.out.println(a+" "+b+" "+c)


    }
    }


    if-else:
    当出现两种情况,不是第一个结果就是第二个结果
    语法:
    if(表达式){
    执行语句;
    }else{
    执行语句;
    }

    执行过程:当程序执行到if的首先会先判断表达式的真假
    若表达式的值是真,就执行if后面{ }的执行语句,然后整个if-else结束,若表达式的值是假,那么就执行else后面{}的执行语句然后整个if-else结束
    代码:
    import java.util.Scanner;
    public class IfElseDemo {
    public static void main(String[] args) {
    System.out.println("请输入两个数:");
    Scanner input = new Scanner(System.in);
    int num1 = input.nextInt();
    int num2 = input.nextInt();

    if(num1 > num2){
    System.out.println(num1);
    }else{ // if ( num1 < num2) 条件
    System.out.println(num2);
    }
    }
    }
    三目运算符(条件,三元运算符)
    ps:java中的三目运算符必须要得到返回值,不然无法使用
    语法:
    表达式 ? 值1 : 值2
    执行过程:先判断表达式的真假,若为真,那么就会将值1返回给调用,若值为假,将值2返回给调用

    if-else if:
    多个条件判断的时候使用
    语法:
    if(表达式1){
    执行语句1;
    }else if (表达式2){
    执行语句2;
    }else if(表达式3){
    执行语句3;
    }else{ //最后一个条件
    执行语句n;
    }
    执行过程:
    当执行到if时,会先判断if后面的表达式的值,若值为真就执行对应{ }中的执行语句.若值为假,就继续向下执行else if判断对应的表达式,直到值为真,若所有的表达式的值都是假的,那么就会执行最后else后面{}里面的执行语句.
    ps:
    1. if-eles if 这种结构的语句,只要有一个表达式为真剩余的条件都不会在判断
    2. 最后的else是可以省略的,else相当于else if 中最后的一个条件,若是要省略else,那么就必须将所有的条件都列举出来
    建议保留else ,这样可以节省一个条件判断

    if-else嵌套
    最好<=3层
    if(表达式){
    if(表达式){
    if(表达式){

    }else{

    }
    }
    }


    switch...case分支
    语法:
    switch(表达式){
    case 常量1:
    执行语句1;
    break;
    case 常量2:
    执行语句2;
    break;
    case 常量 3:
    执行语句3;
    break;
    default
    执行语句 n;
    break;
    }
    表达式: 字符,整数(不支持long),字符串,枚举,算数表达式(一定要有值),绝对不能是boolean类型的表达式
    ps:字符串的支持是JDK1.7之后提供的
    执行过程:
    先计算switch后面()中的值,会和case后面所提供的值进行匹配,若一致,就执行对应的执行语句,然后执行break,整个switch...case就结束,若所有的case都无法匹配,默认执行default,执行完毕,就结束了

    总结:
    若是数值型计算,整数,字符,建议使用switch...case
    若是boolean必须使用if分支

     作业题目:

      1 初级:
      2 1. 下列代码有问题吗?
      3     float score = 3.14; 
      4     byte num = -128;   
      5     char gender = 'nan'; 
      6 float score = 3.14f;
      7 超出byte范围 
      8 char 单个字符串
      9 2. 下列代码的输出结果是?
     10     System.out.println("hello" + "world");
     11     System.out.println(98 + 12);  
     12     System.out.println(13 + "" + 14); 
     13     System.out.println(14 + 'A' + 12);
     14     helloworld
     15      110
     16       1314
     17       91
     18     
     19 3. 下列代码的输出结果是?
     20     int a = 10;
     21     System.out.println(a++);  
     22     System.out.println(++a); 
     23     System.out.println(a--); 
     24     System.out.println(--a); 
     25  10
     26 12
     27 12
     28 10
     29 4:程序设
     30 1:输入两个数,按从小到大排序后输出。 a>b  else(a<b)
     31 import java.util.Scanner;
     32 public class demo12 
     33 {
     34     public static void main(String[] args) 
     35     {
     36         Scanner scan =new Scanner(System.in);
     37         System.out.println("请输入两个值:");
     38         System.out.println("请输入a的值:");
     39         int a =scan.nextInt();
     40         System.out.println("请输入b的值:");
     41         int b =scan.nextInt();
     42         if(a==b){
     43           System.out.println("输出错误");
     44           }
     45         if(a>b){
     46 System.out.println("从小到大结果为:"+b+" "+a);
     47         }else{
     48             System.out.println("从小到大结果为:"+a+" "+b);
     49         }
     50     
     51     }
     52 }
     53 2:输入三个数,并将三个升序输出(升序(从小到大),降序(从大到小))-->集合排序
     54  例如:输入:12  5  3    输出:3   5  12
     55 import java.util.Scanner;
     56 public class demo13 
     57 {
     58     public static void main(String[] args) 
     59     {
     60         Scanner scan =new Scanner(System.in);
     61         System.out.println("请输入三个值:");
     62         System.out.println("请输入a的值:");
     63         int a =scan.nextInt();
     64         System.out.println("请输入b的值:");
     65         int b =scan.nextInt();
     66         System.out.println("请输入c的值:");
     67         int c =scan.nextInt();
     68         if(a>b){
     69         int tmp=a;
     70         a=b;
     71         b=tmp;
     72         }
     73         if(a>c){
     74         int tmp =a;
     75         a=c;
     76         c=tmp;
     77         }
     78         if(b>c)
     79             {
     80         int tmp= b;
     81         b=c;
     82         c=tmp;
     83         }
     84 System.out.println("三个数升序为:" +a+" "+b+" "+c);
     85     }
     86 }
     87 3:输入三个字母,并将它们升序输出  charAt(0)
     88  例如:输入:gae        输出:aeg
     89 import java.util.Scanner;
     90 public class demo14
     91 {
     92     public static void main(String[] args) 
     93     {
     94         Scanner input =new Scanner(System.in);
     95         System.out.println("请输入3个字母:");
     96         char a = input.next().charAt(0);
     97         char b = input.next().charAt(1);
     98         char c = input.next().charAt(2);
     99         if(a>b){
    100         char tmp=a;
    101         a=b;
    102         b=tmp;
    103         }
    104         if(a>c){
    105         char tmp =a;
    106         a=c;
    107         c=tmp;
    108         }
    109         if(b>c)
    110             {
    111         char tmp= b;
    112         b=c;
    113         c=tmp;
    114         }
    115 System.out.println("3个字母升序为:" +a+" "+b+" "+c);
    116     }
    117 }
    118  4:有一个函数,当x<0,则y=-1;当x=0,y=0;当x>0,y=1,;编写一个程序,输入一个x值,输出一个y值。
    119          import java.util.Scanner;
    120 public class demo15 
    121 {
    122     public static void main(String[] args) 
    123     {
    124      Scanner scan =new Scanner(System.in);
    125         System.out.println("请输入x的值:");
    126         int x =scan.nextInt();
    127         if(x<0){
    128         System.out.println("输出y的值为:"+ -1);}
    129         else if(x==0){
    130             System.out.println("输出y的值为:"+ 0);
    131         }else{
    132             System.out.println("输出y的值为:"+ 1);
    133         }
    134         }
    135     }
    136 
    137  5:输入三个数,输出大和最小数的值。 
    138   import java.util.Scanner;
    139 public class demo13 
    140 {
    141     public static void main(String[] args) 
    142     {
    143         Scanner scan =new Scanner(System.in);
    144         System.out.println("请输入三个值:");
    145         System.out.println("请输入a的值:");
    146         int a =scan.nextInt();
    147         System.out.println("请输入b的值:");
    148         int b =scan.nextInt();
    149         System.out.println("请输入c的值:");
    150         int c =scan.nextInt();
    151         if(a>b){
    152         int tmp=a;
    153         a=b;
    154         b=tmp;
    155         }
    156         if(a>c){
    157         int tmp =a;
    158         a=c;
    159         c=tmp;
    160         }
    161         if(b>c)
    162             {
    163         int tmp= b;
    164         b=c;
    165         c=tmp;
    166         }
    167         System.out.println("最小值为:" +a);
    168 System.out.println("最大值为:" +c);
    169     }
    170 }
    171  6:输入一个年份,判断是否是闰年
    172     能被400整除
    173 能被4整除,不能被100整除
    174 import java.util.Scanner;
    175 public class demo16 
    176 {
    177     public static void main(String[] args) 
    178     {
    179         Scanner scan =new Scanner(System.in);
    180         System.out.println("请输入年份:");
    181         int x =scan.nextInt();
    182         if(x%400==0||x%4==0&&x%100!=0){
    183             System.out.println("输出的是闰年:"+x);
    184     }
    185 }
    186 }
    187  7:输入一个字符,判断该字符类型(数字,小写,大写,其他)
    188 import java.util.Scanner;
    189 public class demo17 
    190 {
    191     public static void main(String[] args) 
    192     {
    193         Scanner scan =new Scanner(System.in);
    194         System.out.println("请输入一个字符");
    195         String s = scan.nextLine();
    196         char[] c = s.toCharArray();
    197         for(char ch : c){
    198    if(ch>='0'&&ch<='9'){
    199        System.out.println("输出的字符类型为数字");
    200    }
    201    else if(ch>='a'&&ch<='z'){
    202     System.out.println("输出的字符类型为小写字母");
    203    }
    204    else if(ch>='A'&&ch<='Z'){
    205     System.out.println("输出的字符类型为大写字母");
    206    }else{
    207     System.out.println("输出的字符类型为其他");
    208    }
    209    }
    210 }
    211 }
    212      
    213  8:有一个函数,当x<1,则y=x;当x>=1,y=2*x+1;编写一个程序,输入一个x值,输出一个y值。
    214 import java.util.Scanner;
    215 public class demo17 
    216 {
    217     public static void main(String[] args) 
    218     {
    219      Scanner scan =new Scanner(System.in);
    220         System.out.println("请输入x的值:");
    221         int x =scan.nextInt();
    222         if(x<1){
    223         System.out.println("输出y的值为:"+ x);}
    224 else{
    225             System.out.println("输出y的值为:"+ (2*x+1));
    226         }
    227         }
    228     }
    229 
    230  9:给出一个百分制成绩,要求输出成绩等级,90以上输出‘A’,80~90之间输出‘B’,70~79之间输出‘C’,60~69输出‘D’,60分以下输出‘E’。
    231 import java.util.Random;
    232 public class demo18
    233 {
    234     public static void main(String[] args) 
    235     {
    236         int num=(int)(Math.random()*101);
    237   System.out.println(num);
    238   if(num>=90 ){
    239             System.out.println("A");
    240         }else if(num>=80 ){
    241             System.out.println("B");
    242         }else if(num>=70 ){
    243             System.out.println("C");
    244         }else if(num>=60 ){
    245             System.out.println("D");
    246         }else{
    247             System.out.println("补考");
    248         }
    249    }
    250 }
    251  10:输入12,输出4季度 4 1-3 4-6... 贯穿
    252 import java.util.Scanner;
    253 public class demo19
    254 {
    255     public static void main(String[] args) 
    256     {
    257         System.out.println("请输入一个月份:");
    258         Scanner scan =new Scanner(System.in);
    259         int x =scan.nextInt();
    260         if(1<=x&&x<=3){
    261             System.out.println("春季度");
    262         }
    263         else if(4<=x&&x<=6){
    264         System.out.println("夏季度");
    265         }
    266         else if(7<=x&&x<=9){
    267         System.out.println("秋季度");
    268         }
    269         else if(10<=x&&x<=12) {
    270         System.out.println("冬季度");
    271         }else{
    272         System.out.println("输出月份错误");
    273         }
    274 }
    275 }
    276  11:有4个不等整数,要求按由小到大顺序输出
    277 import java.util.Scanner;
    278 public class demo20
    279 {
    280     public static void main(String[] args) 
    281     {
    282         Scanner scan =new Scanner(System.in);
    283         System.out.println("请输入4个值:");
    284         int a =scan.nextInt();
    285         int b =scan.nextInt();
    286         int c =scan.nextInt();
    287         int d =scan.nextInt();
    288     if(a>b){
    289             int tmp =a;
    290             a=b;
    291             b=tmp;
    292     }
    293     if(a>c){
    294             int tmp=a;
    295             a=c;
    296             c=tmp;
    297     }
    298     if(a>d){
    299             int tmp=a;
    300             a=d;
    301             d=tmp;
    302     }
    303     if(b>c){
    304             int tmp =b;
    305             b=c;
    306             c=tmp;
    307             }
    308     if(b>d){
    309             int tmp =b;
    310             b=d;
    311             d=tmp;
    312             }
    313     if(c>d){
    314             int tmp =c;
    315             c=d;
    316             d=tmp;
    317             }
    318 System.out.println("从小到大排序为" +a+" "+b+" "+c+" "+d);
    319         }
    320     
    321     }
    322  
    323 
    324 12.水仙花数,是一个三位数 个位立方+十位的立方+百位的立方 == 原来的数
    325     153---> 153/100 == 百位
    326 public class demo21 {
    327     public static void main(String[] args) {
    328         int x = 0;        
    329         for(int i=100;i<=999;i++){
    330             int b = i/100;        
    331             int s = (i-100*b)/10;        
    332             int g = (i-s*10-b*100);        
    333             
    334             if(i==g*g*g+s*s*s+b*b*b){
    335                 x++;    
    336                 System.out.print(i+" ");   
    337         }
    338         }System.out.println();        
    339         System.out.println("水仙花数总共有"+x+"个");    
    340     }
    341 }
    342 
    343 13.输入一个时间,输出它的下一秒时间。
    344  例如
    345  输入:20:39:40
    346  输出:20:39:41
    347 
    348       20:39:59
    349       20:40:0
    350 
    351       20:59:59
    352       21:0:0 
    353 
    354       23:59:59
    355       0:0:0
    356  import java.util.Scanner;
    357 public class demo24
    358 {
    359 public static void main(String[] args) 
    360 {
    361 Scanner sc=new Scanner(System.in);
    362 System.out.println("请按hh mm ss的格式输入一个时间");
    363 int hour=sc.nextInt();
    364 int minute=sc.nextInt();
    365 int second=sc.nextInt();
    366 System.out.println("当前时间为:"+hour+":"+minute+":"+second);
    367 //计算下一秒
    368 second++;
    369 //修正时间
    370 if(second>=60){
    371  second=0;
    372  minute++;
    373  if(minute>=60){
    374    minute=0;
    375 hour++;
    376 if(hour>=24){
    377  hour=0;
    378 }
    379  }
    380 }
    381 //输出
    382 System.out.println("下一秒为:"+hour+":"+minute+":"+second);
    383 }
    384 }
    385 
    386  14.输入一个日期,输出这一天是这一年的第几天,考虑平年闰年
    387  输入:2000-1-1                                输出1天
    388  输入:2000-12-31                   输出366天
    389         
    390   2000 - 5- 1 --.> 1,+2,+3+,4+1
    391 import java.util.Scanner;
    392 
    393 public class demo25 {
    394   
    395     public static void main(String[] args) {
    396         int year;
    397         int mouth;
    398         int day=0;
    399         int days;
    400         int d=0;
    401         int e = 0;
    402         Scanner scanner = new Scanner(System.in);
    403         do {
    404             System.out.println("输入年:");
    405             year = scanner.nextInt();
    406             System.out.println("输入月:");
    407             mouth = scanner.nextInt();
    408             System.out.println("输入日:");
    409             days = scanner.nextInt();
    410             if (mouth < 0 || mouth > 12 || days < 0 || days > 31) {
    411                 System.out.println("input error!");
    412                 e = 1;
    413             }
    414         } while (e == 1);
    415         for (int i = 1; i <mouth; i++) {
    416             switch (i) {
    417             case 1:
    418             case 3:
    419             case 5:
    420             case 7:
    421             case 8:
    422             case 10:
    423             case 12: {
    424                 day = 31;
    425                 break;
    426             }
    427             case 4:
    428             case 6:
    429             case 9:
    430             case 11: {
    431                 day = 30;
    432                 break;
    433             }
    434             case 2: {
    435               
    436                 if ((year % 100 !=0 &&year % 4 == 0) || (year % 100 == 0 && year%400==0)) {
    437                     day = 29;
    438                 } else {
    439                     day = 28;
    440                 }
    441             }
    442             default:
    443                 break;
    444             }
    445             d+=day;
    446         }
    447         System.out.println("这是"+year+"年的"+(d+days)+"天");
    448     }
    449 }
  • 相关阅读:
    HTTP 错误 404.2
    SQL Server 2008 R2如何开启数据库的远程连接(转)
    CSS中font-family:中文字体对应的英文名称
    15/18位身份证号码正则表达式(详细版)
    C#获取系统时间及时间格式
    C#正则表达式判断输入日期格式是否正确
    Linux查看机器负载
    模拟HTTP请求超时时间设置
    MySQL show命令的用法
    innodb事务隔离级别
  • 原文地址:https://www.cnblogs.com/lijun199309/p/9405055.html
Copyright © 2020-2023  润新知