JAVA运算符:%,++ 需要注意的地方

本文详细解析了Java中求余运算符(%)的特点及其运算结果符号的决定因素,并通过实例展示了前置与后置递增(++)运算符的不同用法及结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、求余符号:%

1.  public class Student{

2.        public static void main(String[] args){

3.                int i1 = 12 % 5;

4.                int i2 = -12 % 5;

5.                int i3 = 12 % (-5);

6.                int i4 = -12 % (-5);

7.                System.ou.println("以此输出i1,i2,i3,i4");

8.         }

9.  }

        运行后可得答案 2,-2, 2 ,-2。

        由此可得,求余运算符最终结果的符号,由被余数。

二、‘++’运算符

1. public class Student{

 2.          public static void main(String[] args){

 3.                       int myInt1 = 10;

 4.                       int myInt2 = myInt1++;//后++

5.                        System.out.println(myInt1);// 结果为 11

6.                        System.out.println(myInt2);// 结果为 10
7. //**********************

9.                        int myInt3 = 10;

10.                      int myInt4 = ++myInt3;//前++

11.                      System.out.println(myInt3); // 结果为 11

12.                      System.out.println(myInt4); // 结果为 11

13.        }

14.     } 

可以看出,‘++’放在变量前后有不同的效果。




import java.util.Scanner; public class StudentGrade { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 1. 输入学生学号、姓名、以及三门课成绩 System.out.print("请输入学生学号:"); long studentId = scanner.nextLong(); System.out.print("请输入学生姓名:"); String studentName = scanner.next(); double[] scores = new double[3]; System.out.print("请依次输入Java、数据结构、英语成绩:"); for (int i = 0; i < 3; i++) { scores[i] = scanner.nextDouble(); } // 2. 计算总成绩、平均分 double totalScore = 0; for (double score : scores) { totalScore += score; } double averageScore = totalScore / 3; // 3. 成绩评测 String evaluation; if (averageScore >= 90) { evaluation = "优秀"; } else if (averageScore >= 80) { evaluation = "良好"; } else if (averageScore >= 70) { evaluation = "中等"; } else if (averageScore >= 60) { evaluation = "及格"; } else { evaluation = "不及格"; } System.out.println("评级:" + evaluation); // 4. 查找成绩 System.out.print("请输入要查找的成绩:"); double targetScore = scanner.nextDouble(); boolean found = false; for (double score : scores) { if (score == targetScore) { System.out.println("存在该成绩"); found = true; break; } } if (!found) { System.out.println("不存在该成绩"); } // 5. 查询科目成绩 System.out.print("请输入要查询成绩的科目(1代表Java,2代表数据结构,3代表英语):"); int subject = scanner.nextInt(); switch (subject) { case 1: System.out.println("Java成绩为:" + scores[0]); break; case 2: System.out.println("数据结构成绩为:" + scores[1]); break; case 3: System.out.println("英语成绩为:" + scores[2]); break; default: System.out.println("输入的科目编号错误"); } // 6. 格式化输出学生信息 System.out.printf("学号:%d | 姓名:%s | 成绩:[%f, %f, %f] | 总成绩:%.0f | 平均分:%.2f | 评级:%s\n", studentId, studentName, scores[0], scores[1], scores[2], totalScore, averageScore, evaluation); scanner.close(); } }将求总成绩,平均分,评级,成绩查找,格式化输出都重构为方法,并在主函数调用
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值