一些基础的java编程代码

JAVA基础代码

1、强制类型转换

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

2、判断是否为闰年

//判断是否为闰年 闰年要能取余4但是不能取余100,取余400的也可以
@Test
public 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、交换数字

@Test
public 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折

@Test
public 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、生成随机数并猜数

@Test
public 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 < random) {
System.out.println("小了");
}
if (caice == random) {
System.out.println("随机数为" + random);
System.exit(0);//退出循环
}
}
System.out.println("结束");
}

6、乘法表

@Test
public void work5() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
int sum = i * j;
System.out.print(i + "*" + j + "=" + sum + " ");
if (i == j)
System.out.println();
}
}

}```

## 7、年份,硬写版,实际上31天的可以放一起用一个break 例如case1: case3: break;

                   
```java
@Test
public 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;
}
}

8、打印

//打印1到12345

@Test
public void work7() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
System.out.print(j);
if (i == j) {
System.out.println();
}
}
}
}
//打印菱形笨办法,可以打印空格,然后打印*号

@Test
public void work8() {
for (int i = 1; i <= 6; i++) {
//每行打印的空格数
for (int j = 6; j >= i; j--) {
System.out.print(" ");
}
//打印左半边的*
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
//打印右半边的*,因为此时第一行不用打印*,所以,j<i;
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}

9、奇数偶数和

@Test
public void work9() {
int sum1 = 0, sum2 = 0, i;
for (i = 1; i <= 100; i++) {
if (i % 2 == 0) {
sum2 = sum2 + i;
} else {
sum1 = sum1 + i;
}
}
System.out.println("奇数和 =" + sum1 + "偶数和 =" + sum2);
}

10、1000内被5整除的数

@Test
public void work10() {
int i, flag = 0;
for (i = 1; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.print(i + " ");
flag++;
if (flag == 3) {
flag = 0;
System.out.println();
}
}
}
}

11、9的阶乘

@Test
public void work11() {
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);
}
}
}

12、素数的查找(开平方版)

@Test
public void work12() {
int i, j, count = 0;
for (i = 100; i <= 200; i++) {
for (j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
break;
}

}
if (j > Math.sqrt(i)) {
System.out.println(i + " ");
count++;
}
}
System.out.println("素数的个数为 " + count + "个");

}

13随机数,Math版

@Test
public void work13() {
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 < 0) {
System.out.println("请输入合法数据");
continue;
}
if (a > random) {
System.out.println("太大了");
} else if (a < random) {
System.out.println("太小了");
} else if (a == random) {
System.out.println("恭喜你,猜对了! 随机数是 " + random);
System.exit(0);
}
}

}

14、月份,年份查询

@Test
public void work14() {
Scanner scanner = new Scanner(System.in);
System.out.println("请依次输入年份,月份");
int year = scanner.nextInt();
int month = scanner.nextInt();
int flag = 0;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println("是闰年");
flag = 1;
} else {
System.out.println("是平年");
flag = 0;
}
if (month == 2 && flag == 1) {
month = 13;
}
switch (month) {
case 1:
System.out.println("1月有31天");
break;
case 2:
System.out.println("2月有28");
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;
case 13:
System.out.println("2月有29天");
break;
default:
break;
}

}

15、Switch版成绩查询

@Test
public void work15() {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();

if (score < 0 || score > 100) {
System.out.println("非法数据!");
return;
}

switch (score / 10) {
case 9:
case 10:
System.out.println("优秀");
break;
case 8:
System.out.println("良好");
break;
case 7:
System.out.println("一般");
break;
case 6:
System.out.println("及格");
break;
default:
System.out.println("不及格");
break;
}
}

16、打印菱形(新)

@Test
public void zifu() {
for (int i = 1; i <=6 ; i++) {
for(int j=1;j<=(6-i);j++){
System.out.print(" ");
}
for(int j=1;j<=(2*i-1);j++){
System.out.print("*");
}
System.out.println();
}

}

}
  • 29
    点赞
  • 256
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值