首页 资讯▪软件 技术•资讯 Java作业基础知识梳理-

Java作业基础知识梳理-

[  发表时间:2021-01-07 20:04:04    信息来源:九剑网络  ]

来源:https://blog.csdn.net/m0_48355416/article/details/112201464?utm_medium=distribute.pc_category.none-task-blog-hot-2.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-2.nonecase&request_id=

1.  Java中常用的赋值运算符

+= 将该运算符左边的数值加上右边的数值, 其结果赋值给左边变量本身

-= 将该运算符左边的数值减去右边的数值, 其结果赋值给左边变量本身

*= 将该运算符左边的数值乘以右边的数值, 其结果赋值给左边变量本身

/= 将该运算符左边的数值整除右边的数值, 其结果赋值给左边变量本身

%= 将该运算符左边的数值除以右边的数值后取余,其结果赋值给左边变量本身

2.Java注释:

分为单行注释,多行注释(块注释),文档注释

注释可以提高代码可读性,方便后期代码维护,

方便程序员间的交流沟通生成帮助文档。

同时注释不能长篇大论,也不能过于简单。

3.switch语句

switch中的值只能是整数、枚举、字符、字符串

不可使用long、float、double、boolean作为它的数据类型

4.交换三个值(代码):

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int a = scan.nextInt();

        int b = scan.nextInt();

        int tmp = a;

        a = b;

        b = tmp;

        System.out.println("a = " + a);

        System.out.println("b = " + b);

    }

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

求三个数中的最值(代码):

public static void main(String[] args) {

    int a = 4;

    int b = 2;

    int c = 3;

    int max = 0;

    int min = 0;

    if (a > b && a > c){

        max = a;

    }

    else if (b > a && b > c){

        max = b;

    }

    else

        max = c;

    if (a < b && a < c) {

        min = a;

    }

    else if (b < a && b < c) {

        min = b;

    }

    else

        min = c;

    System.out.println("max = " + max);

    System.out.println("min = " + min);

}

•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

5.编写程序数一下 1到 100 的所有整数中出现多少个数字9

public static void main(String[] args) {

    int count = 0;

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

        if(i % 10 == 9) {

            count++;

        }else if(i / 10 == 9) {

            count++;

        }

    }

    System.out.println(count);

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

6.输出 1000 - 2000 之间所有的闰年

public static void main(String[] args) {

    for(int year = 1000;year <= 2000;year++){

        if((year % 4 == 0 && year % 100 != 0) || year % 400 ==0){

            System.out.println(year);

        }

    }

}

•1

•2

•3

•4

•5

•6

•7

7.打印 1 - 100 之间所有的素数

public static void main(String[] args) {

    int count = 0;

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

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

            if (i % j == 0 && i != j) 

                break;

            if (j == i) {

                if (count % 5 == 0)

                    System.out.println();

                System.out.print(j + " ");

                count++;

            }

        }

    }

    System.out.println("\n素数有" + count + "个");

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

•14

•15

•16

8.给定一个数字,判定一个数字是否是素数

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int num = scanner.nextInt();

    for (int j = 0; j <= num; j++) {

        int i = 2;

        for (; i <= Math.sqrt(j); i++) {

            if (num % i == 0) {

                System.out.println("不是素数");

                break;

            }

        }

        if (i > Math.sqrt(num)) {

            System.out.println("是素数");

            break;

        }

    }

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

•14

•15

•16

•17

9.根据输入的年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)(这道题好像有瑕疵,没得满分,有缘人帮忙看看)

public static void main(String[] args) {

    while(true){

        System.out.println("请输入要判断的年龄");

        Scanner in=new Scanner(System.in);

        int age=in.nextInt();

        if(age<0){

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

            continue;

        }

        if(age>=0&&age<=18){

            System.out.println("少年");

            break;

        }

        if(age>=19&&age<=28){

            System.out.println("少年");

            break;

        }

        if(age>=29&&age<=55){

            System.out.println("中年");

            break;

        }

        else{

            System.out.println("老年");

            break;

        }

    }

}

•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

10.打印 X 图形

public static void printX(int m) {

    for (int x = 1; x <= m; x++) {

        for (int y = 1; y <= m; y++) {

            if (x == y || x + y == m + 1) {

                System.out.print("*");

            } else {

                System.out.print(" ");

            }

        }

        System.out.println();

    }

}

public static void main(String[] args) {

    printX(10);

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

•14

•15

11.完成猜数字游戏 ,用户输入数字,判断该数字是大于,小于,还是等于随机生成的数字,等于的时候退出程序。

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    Random random = new Random();

    int ranNum = random.nextInt(100) + 1;

    while (true){

        int num = scanner.nextInt();

        if (num < ranNum) {

            System.out.println("小");

        }else if (num > ranNum) {

            System.out.println("大");

        }else{

            System.out.println("==");

            break;

        }

    }

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

•14

•15

•16

1.求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本 身,如;153=1+5+3?,则153是一个“水仙花数“。)

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();

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

        int count = 0;

        int tmp = i ;

        while (tmp != 0) {

            count++;

            tmp = tmp/10;

        }

        tmp = i;

        int sum = 0;

        while (tmp != 0) {

            sum += Math.pow(tmp%10,count);

            tmp = tmp/10;

        }

        if(sum == i) {

            System.out.println(i);

        }

    }

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

•14

•15

•16

•17

•18

•19

•20

13.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值 。

public class Main {

    public static double func(int i,int j) {

        double ret = 1.0/i - 1.0/j;

        return ret;

    }

    public static void main(String[] args) {

        double sum = 0.0;

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

            sum = sum+ func(i,i+1);

        }

        System.out.println(sum);

    }

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

14.求两个正整数的最大公约数

public static void main(String[] args) {

    int a = 9;

    int b = 18;

    int c = 0;

    while (a % b != 0) {

        c = a %b;

        a = b;

        b = c;

    }

    System.out.println(c);

}


•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

15.求一个整数,在内存当中存储时,二进制1的个数。

public static void main(String[] args) {

 

    Scanner scanner = new Scanner(System.in);

    int num = scanner.nextInt();

    int count = 0;

    for(int i = 0;i < 32;i++) {

        if(((num >> i) & 1) == 1) {

            count++;

        }

    }

    System.out.println(count);

  }

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

16.获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列

public static void main(String[] args) {

    int num = 14;

    odd(num);

    System.out.println();

    even(num);

}

public static void odd(int num) {

    for (int i = 30; i >= 0; i = i - 2) {

        if ((num & (1 << i)) != 0) {

            System.out.print("1 ");

        } else {

            System.out.print("0 ");

        }

    }

}

public static void even(int num) {

    for (int i = 31; i >= 0; i = i - 2) {

        if ((num & (1 << i)) != 0) {

            System.out.print("1 ");

        } else {

            System.out.print("0 ");

        }

    }

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

•14

•15

•16

•17

•18

•19

•20

•21

•22

•23

•24

17.编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序

public static void main(String[] args) {

    int count = 3;

    Scanner scanner = new Scanner(System.in);

    while (count  != 0) {

        String passWorld = scanner.nextLine();

        if(passWorld.equals("123456")) {

            System.out.println("登录成功!");

        }else {

            count--;

        }

    }

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

18.输出一个整数的每一位,如:123的每一位是1 , 2 , 3

public static void  print (int n) {

    if(n < 0) {//变成整数。

        System.out.println("-");

        n = -n;

    }

    if(n>9) {

        print(n/10);//递归

    }

    System.out.println(n%10);//打印个位数

}

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int n = scan.nextInt();

    print(n);

}

•1

•2

•3

•4

•5

•6

•7

•8

•9

•10

•11

•12

•13

•14

•15

•16

19.输出n*n的乘法口诀表,n由用户输入。

public static void main(String[] args) {

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

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

            System.out.print(i+"*"+j + "=" + i*j +" ");

        }

        System.out.println();

    }

}


声明:本网(www.9jit.com)所刊载的所有信息,包括文字、图片、课件、软件、声音、相片、视频、图表,广告、商业信息及电子邮件的全部内容,除特别标明之外,版权归九剑IT网站所有。未经本网的明确书面许可,任何单位或个人不得以任何方式作全部或局部复制、转载、引用,再造或创造与该内容有关的任何派生产品,否则本网将追究其法律责任。 本网凡特别注明稿件来源的文/图等稿件为转载稿,本网转载出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性。如对稿件内容有疑议,请及时与我们联系。 如本网转载稿涉及版权问题,请作者在两周内速来电或来函与我们联系,我们将及时按作者意愿予以更正。
× 关注公众号 送VIP邀请码

关注公众号 送VIP邀请码

会员登陆

captcha
下次自动登陆 忘记密码?
×
—— 还没帐号?点击注册! ——