小工具知识网

当前位置: 首页 > 行业知识 > 一些基础的java编程代码

行业知识

一些基础的java编程代码

2026-11-16 16:46:48 来源:互联网转载

JAVA基础代码

文章目录

  • JAVA基础代码
    • 1、强制类型转换
    • 2、判断是否为闰年
    • 3、交换数字
    • 4、单价,数量,金额,满500打8折
    • 5、生成随机数并猜数
    • 6、乘法表
    • 8、打印
    • 9、奇数偶数和
    • 10、1000内被5整除的数
    • 11、9的阶乘
    • 12、素数的查找(开平方版)
    • 13随机数,Math版
    • 14、月份,年份查询
    • 15、Switch版成绩查询
    • 16、���印菱形(新)

      1、强制类型转换

      public class Day1 {//强制类型转换public static void main(String[] args) {byte a = (byte) 257; //超出最大额度强制类型转换会从头开始计数System.out.println(a);}

      2、判断是否为闰年

      //判断是否为闰年 闰年要能取余4但是不能取余100,取余400的也可以@Testpublic void work1() {Scanner scanner = new Scanner(System.in);int year = scanner.nextInt();if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {System.out.println("是闰年");} else {System.out.println("是平年");}}

      3、交换数字

      @Testpublic void work2() {int a, b, c;Scanner scanner = new Scanner(System.in);a = scanner.nextInt();b = scanner.nextInt();System.out.println("数字1是:" + a + " 数字是2:" + b);c = a;a = b;b = c;System.out.println("数字1是:" + a + " 数字2是:" + b);}

      4、单价,数量,金额,满500打8折

      @Testpublic void work3() {int b;double a, c, d, e;Scanner scanner = new Scanner(System.in);System.out.println("请输入单价");a = scanner.nextDouble();System.out.println("请输入数量");b = scanner.nextInt();System.out.println("请输入金额");c = scanner.nextDouble();if (a * b >= 500) {d = a * b * 0.8;if (d > c) {System.out.println("钱不够哦,亲");return;}} else {d = a * b;if (d > c) {System.out.println("钱不够哦,亲");return;}}e = c - d;System.out.println("应收金额 " + d + " 找零 " + e);}

      5、生成随机数并猜数

      @Testpublic void work4() {int random = new Random().nextInt(1000);int caice = 0;Scanner scanner = new Scanner(System.in);while (scanner.hasNextInt()) {caice = scanner.nextInt();if (caice > random) {System.out.println("大了");} else if (caice  

      6、乘法表

      @Testpublic void work5() {for (int i = 1; i for (int j = 1; j int sum = i * j;System.out.print(i + "*" + j + "=" + sum + " ");if (i == j)System.out.println();}}}```## 7、年份,硬写版,实际上31天的可以放一起用一个break 例如case1: case3: break;                   ```java@Testpublic void work6() {int i;Scanner scanner = new Scanner(System.in);i = scanner.nextInt();switch (i) {case 1:System.out.println("1月有31天");break;case 2:System.out.println("2月有28/29天");break;case 3:System.out.println("3月有31天");break;case 4:System.out.println("4月有30天");break;case 5:System.out.println("5月有31天");break;case 6:System.out.println("6月有30天");break;case 7:System.out.println("7月有31天");break;case 8:System.out.println("8月有31天");break;case 9:System.out.println("9月有30天");break;case 10:System.out.println("10月有31天");break;case 11:System.out.println("11月有30天");break;case 12:System.out.println("12月有31天");break;default:break;}}int i, j;for (i = 1; i for (j = 1; j System.out.print(j);if (i == j) {System.out.println();}}}}//打印菱形笨办法,可以打印空格,然后打印*号@Testpublic void work8() {for (int i = 1; i //每行打印的空格数for (int j = 6; j = i; j--) {System.out.print(" ");}//打印左半边的*for (int j = 1; j System.out.print("*");}//打印右半边的*,因为此时第一行不用打印*,所以,jSystem.out.print("*");}System.out.println();}}int sum1 = 0, sum2 = 0, i;for (i = 1; i if (i % 2 == 0) {sum2 = sum2 + i;} else {sum1 = sum1 + i;}}System.out.println("奇数和 =" + sum1 + "偶数和 =" + sum2);}int i, flag = 0;for (i = 1; i if (i % 5 == 0) {System.out.print(i + " ");flag++;if (flag == 3) {flag = 0;System.out.println();}}}}int sum = 1;for (int i = 9; i = 1; i--) {sum = sum * i;if (i != 1) {System.out.print(i + "*");}if (i == 1) {System.out.println(i + "=" + sum);}}}int i, j, count = 0;for (i = 100; i for (j = 2; j if (i % j == 0) {break;}}if (j  Math.sqrt(i)) {System.out.println(i + " ");count++;}}System.out.println("素数的个数为 " + count + "个");}int random = (int) (Math.random() * 1000 + 1);System.out.println(random);Scanner scanner = new Scanner(System.in);System.out.println("请输入猜测: ");while (scanner.hasNextInt()) {int a = scanner.nextInt();if (a == 0) {System.out.println("程序退出");break;}if (a  1000 || a 

上一篇:揭秘抢购活动策划秘籍,公众号商家如何吸引顾客,激增销量!

下一篇:mip域名怎么买