题目:
一、基础语法题
- 要求:编写一个 Java 程序,定义一个名为Circle的类。该类包含一个私有成员变量radius(半径),数据类型为double。提供一个构造方法用于初始化半径。编写一个getArea方法,用于计算并返回圆的面积(公式:S=πr2,π取值为Math.PI)。在main方法中创建Circle类的对象,半径设为 5.0,调用getArea方法并输出结果。
- 目标:熟悉类的定义、成员变量、构造方法及方法的调用,掌握基本数学公式在 Java 中的实现。
- 要求:使用switch – case语句编写一个程序,根据用户输入的数字(1 – 7)输出对应的星期几。例如,输入 1 输出 “星期一”,输入 2 输出 “星期二”,以此类推。如果输入的数字不在 1 – 7 范围内,输出 “输入错误”。假设使用Scanner类获取用户输入。
- 目标:熟练掌握switch – case语句的使用,学会处理用户输入及异常情况。
二、数组与字符串操作题
- 要求:定义一个字符串数组strArray,包含 5 个字符串元素。编写程序,遍历该数组,统计每个字符串中字符'a'出现的次数,并将结果输出。例如,对于字符串 “banana”,输出 “字符 'a' 出现次数: 3”。
- 目标:掌握数组的遍历,以及字符串中字符查找和计数的操作。
- 要求:给定一个整数数组nums,其元素为{12, 34, 56, 78, 90}。编写一个方法reverseArray,将数组元素顺序反转。在main方法中调用该方法,并输出反转后的数组元素。
- 目标:学会数组元素的反转操作,理解方法参数传递及数组操作对原数组的影响。
三、面向对象进阶题
- 要求:创建一个继承体系,定义一个Animal类作为基类,包含私有成员变量name(动物名称),并提供构造方法和getName方法。再定义Dog类和Cat类继承自Animal类。Dog类增加一个bark方法,输出 “汪汪汪”;Cat类增加一个meow方法,输出 “喵喵喵”。在main方法中创建Dog和Cat类的对象,并分别调用它们特有的方法和从基类继承的getName方法。
- 目标:理解类的继承关系,掌握子类对父类成员的继承和扩展,以及对象的多态性体现。
- 要求:设计一个BankAccount类,包含私有成员变量accountNumber(账号,String类型)、balance(余额,double类型)。提供构造方法初始化账号和余额。添加deposit方法用于存款(增加余额),withdraw方法用于取款(减少余额,但要检查余额是否足够,若不足则输出 “余额不足”)。创建一个Customer类,包含私有成员变量customerName(客户姓名)和一个BankAccount类型的成员变量account。在Customer类中提供方法showAccountInfo,用于显示客户姓名、账号及余额。在main方法中创建Customer对象,进行存款、取款操作,并调用showAccountInfo方法显示账户信息。
- 目标:综合运用类的组合、封装等面向对象特性,实现一个具有一定业务逻辑的程序。
四、异常处理与文件操作题
- 要求:编写一个程序,从一个文本文件(假设文件名为numbers.txt,文件中每行包含一个整数)中读取整数,并计算这些整数的平均值。如果文件不存在,捕获FileNotFoundException异常并输出 “文件未找到”。如果文件中的某一行内容不是有效的整数,捕获NumberFormatException异常并输出 “数据格式错误”。最后无论是否发生异常,都要关闭文件流。
- 目标:掌握文件读取操作,学会捕获和处理不同类型的异常,以及确保资源正确关闭。
参考答案:
一、基础语法题
题目 1 答案
class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
Circle circle = new Circle(5.0);
System.out.println("圆的面积为:" + circle.getArea());
}
}
题目 2 答案
import java.util.Scanner;
public class WeekdayCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入数字(1-7):");
int num = scanner.nextInt();
switch (num) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("输入错误");
}
scanner.close();
}
}
二、数组与字符串操作题
题目 1 答案
public class CharCounter {
public static void main(String[] args) {
String[] strArray = {"apple", "banana", "grape", "orange", "kiwi"};
for (String str : strArray) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'A') {
count++;
}
}
System.out.println("字符串 \\"" + str + "\\" 中 'a' 出现次数: " + count);
}
}
}
题目 2 答案
public class ArrayReverser {
public static void reverseArray(int[] nums) {
int left = 0;
int right = nums.length – 1;
while (left < right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
right–;
}
}
public static void main(String[] args) {
int[] nums = {12, 34, 56, 78, 90};
reverseArray(nums);
for (int num : nums) {
System.out.print(num + " ");
}
}
}
三、面向对象进阶题
题目 1 答案
java
class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void bark() {
System.out.println("汪汪汪");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void meow() {
System.out.println("喵喵喵");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Dog dog = new Dog("旺财");
Cat cat = new Cat("咪咪");
System.out.println("狗的名字:" + dog.getName());
dog.bark();
System.out.println("\\n猫的名字:" + cat.getName());
cat.meow();
}
}
题目 2 答案
java
class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("余额不足");
}
}
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
}
class Customer {
private String customerName;
private BankAccount account;
public Customer(String customerName, BankAccount account) {
this.customerName = customerName;
this.account = account;
}
public void showAccountInfo() {
System.out.println("客户姓名:" + customerName);
System.out.println("账号:" + account.getAccountNumber());
System.out.println("余额:" + account.getBalance());
}
}
public class BankSystem {
public static void main(String[] args) {
BankAccount account = new BankAccount("123456", 1000.0);
Customer customer = new Customer("张三", account);
customer.showAccountInfo();
account.deposit(500.0);
account.withdraw(200.0);
System.out.println("\\n操作后:");
customer.showAccountInfo();
}
}
四、异常处理与文件操作题
题目答案
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileNumberProcessor {
public static void main(String[] args) {
int sum = 0;
int count = 0;
double average;
try (BufferedReader reader = new BufferedReader(new FileReader("numbers.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
try {
int num = Integer.parseInt(line.trim());
sum += num;
count++;
} catch (NumberFormatException e) {
System.out.println("数据格式错误: " + line);
}
}
if (count == 0) {
System.out.println("文件中没有有效数字");
return;
}
average = (double) sum / count;
System.out.println("平均值为:" + average);
} catch (FileNotFoundException e) {
System.out.println("文件未找到");
} catch (IOException e) {
System.out.println("读取文件时出错");
}
}
}
答案说明:
建议在 IDE 中运行时注意:
- 文件操作题需要确保文件路径正确
- 异常处理题可手动创建测试文件 numbers.txt
- 综合题可通过逐步调试理解执行流程
网硕互联帮助中心



评论前必须登录!
注册