一些基础的java编程代码

这篇具有很好参考价值的文章主要介绍了一些基础的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();
}

}

}

文章来源地址https://www.toymoban.com/news/detail-709678.html

到了这里,关于一些基础的java编程代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包