1. 循环与流程控制综合题
【题目】编写程序:输出100以内能被3整除且个位数为6的所有整数,并统计其个数。
【思路】首先明确范围:1~99(100以内不含100);其次条件:①能被3整除 → n % 3 == 0;②个位为6 → n % 10 == 6;两条件需同时满足,采用逻辑与运算符&&。
public class DivisibleBy3With6 {
public static void main(String[] args) {
int count = 0;
System.out.print("满足条件的数字:");
for (int i = 1; i <= 99; i++) {
if (i % 3 == 0 && i % 10 == 6) {
System.out.print(i + " ");
count++;
}
}
System.out.println("\n总个数:" + count);
}
}
【运行结果】
满足条件的数字:6 36 66 96
总个数:4
【拓展思考】若题目改为“1000以内个位为6且能被3整除的数”,只需将循环上限改为1000;若要求“十位为6”,则需判断 (i / 10) % 10 == 6。此处涉及整除与取模的组合应用,是理解数字分解的关键。
【易错点】①忽略“100以内”是否包含100(本题不含);②误用||导致逻辑错误;③循环起始值设为0导致0被误判(0 % 10 == 0 ≠ 6,但逻辑冗余)。
2. 面向对象设计题
【题目】定义Student类,包含属性:姓名(String)、学号(int)、成绩(double);方法:构造方法(初始化三属性)、display()(输出所有信息)、getGradeLevel()(根据成绩返回等级:≥90为A,80~89为B,70~79为C,60~69为D,<60为E)。
public class Student {
private String name;
private int id;
private double score;
public Student(String name, int id, double score) {
this.name = name;
this.id = id;
this.score = score;
}
public void display() {
System.out.println("姓名:" + name + ",学号:" + id + ",成绩:" + score + ",等级:" + getGradeLevel());
}
public String getGradeLevel() {
if (score >= 90) return "A";
else if (score >= 80) return "B";
else if (score >= 70) return "C";
else if (score >= 60) return "D";
else return "E";
}
public static void main(String[] args) {
Student s1 = new Student("张三", 1001, 88.5);
Student s2 = new Student("李四", 1002, 92.0);
s1.display(); // 姓名:张三,学号:1001,成绩:88.5,等级:B
s2.display(); // 姓名:李四,学号:1002,成绩:92.0,等级:A
}
}
【设计要点】①封装性:属性私有化,通过构造器初始化;②方法职责单一:getGradeLevel()仅负责等级判断;③使用else if链避免重复判断,提升效率。
【常见误区】①构造器中未初始化属性导致空指针;②getGradeLevel()未覆盖所有分数区间(如漏掉60分边界);③成绩未做范围校验(负分或超100分)。
【变式训练】增加setScore(double score)方法并校验分数范围(0~100),否则抛出IllegalArgumentException。
3. 字符串处理题
【题目】编写程序:输入一个字符串,统计其中每个字符(区分大小写)出现的次数,并按字符升序输出结果。
【思路】使用HashMap存储字符及其计数;遍历字符串,将每个字符作为键,计数作为值;最后对键集排序后输出。
import java.util.*;
public class CharFrequency {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入字符串:");
String input = sc.nextLine();
sc.close();
Map map = new HashMap<>();
for (char c : input.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
List keys = new ArrayList<>(map.keySet());
Collections.sort(keys);
System.out.println("字符统计结果:");
for (char key : keys) {
System.out.println(key + ":" + map.get(key));
}
}
}
【运行示例】
输入:aAbB
输出:
A:1
a:1
B:1
b:1
【关键点】getOrDefault(key, defaultValue)简化计数逻辑;toCharArray()高效转换字符串为字符数组;Collections.sort()对键集排序。
【拓展】若要求忽略大小写统计,可统一转小写:map.put(Character.toLowerCase(c), ...)。
4. 数组操作题
【题目】给定一个整型数组,找出其中最大的两个数(允许相等),并输出其值及索引位置。
【思路】遍历数组,维护两个变量:max1(最大值)与max2(第二大值)及对应索引;需处理初始值(设为负无穷或数组前两个元素)及相等情况。
public class TwoMax {
public static void main(String[] args) {
int[] arr = {5, 9, 3, 9, 1, 7};
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE;
int idx1 = -1, idx2 = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max1) {
max2 = max1; idx2 = idx1;
max1 = arr[i]; idx1 = i;
} else if (arr[i] > max2) {
max2 = arr[i]; idx2 = i;
}
}
System.out.println("最大值:" + max1 + ",索引:" + idx1);
System.out.println("第二大值:" + max2 + ",索引:" + idx2);
}
}
【输出】最大值:9,索引:1
第二大值:9,索引:3
【注意】该方法时间复杂度O(n),空间O(1),优于排序法O(n log n)。