1.简答题 定义一个学生类Studentjava,成员变量包括na me(姓名)、age(年龄)、sex(性别)是成员变量,成员方法包括read()读书、run(()跑步。并实例化对象stu1、stu2为成员变量赋值,并使用对象调用成员方法。
时间: 2024-05-22 18:16:27 浏览: 8
定义一个学生类Student.java的代码如下:
public class Student {
String name; // 姓名
int age; // 年龄
String sex; // 性别
public void read() { // 读书方法
System.out.println(name + "正在读书");
}
public void run() { // 跑步方法
System.out.println(name + "正在跑步");
}
}
然后实例化对象stu1和stu2,并为成员变量赋值,代码如下:
Student stu1 = new Student();
stu1.name = "张三";
stu1.age = 18;
stu1.sex = "男";
Student stu2 = new Student();
stu2.name = "李四";
stu2.age = 20;
stu2.sex = "女";
最后使用对象调用成员方法,代码如下:
stu1.read(); // 张三正在读书
stu2.run(); // 李四正在跑步
相关问题
1. 简答题 定义一个字符串,计算字符串中大写字母、小写字母、数字的个数并输出。
首先,我们需要定义一个字符串并统计其中大写字母、小写字母和数字的个数。
可以使用 Python 的内置函数 isupper()、islower() 和 isdigit() 来判断一个字符是否为大写字母、小写字母和数字。
下面是一个示例代码:
```python
string = "Hello World 123"
upper_count = 0
lower_count = 0
digit_count = 0
for char in string:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
elif char.isdigit():
digit_count += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
```
输出结果:
```
大写字母个数: 2
小写字母个数: 8
数字个数: 3
```
这里我们定义了一个字符串变量 `string`,并使用了三个计数器变量 `upper_count`、`lower_count` 和 `digit_count` 来分别统计大写字母、小写字母和数字的个数。
然后我们使用 for 循环遍历字符串中的每个字符,如果字符是大写字母,则将 `upper_count` 计数器加 1,如果是小写字母,则将 `lower_count` 计数器加 1,如果是数字,则将 `digit_count` 计数器加 1。
最后输出三个计数器变量的值即可。
1. 简答题定义一个数组,输入10个数,排序输出。提交源代码,写出操作步骤,运行
好的,这是一个简单的排序算法题目,下面是操作步骤和源代码:
1. 首先定义一个长度为10的整型数组。
2. 然后循环输入10个数到数组中。
3. 使用冒泡排序算法对数组进行排序。
4. 输出排序后的数组。
下面是完整的源代码:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[10], i;
printf("请输入10个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, 10);
printf("排序后的数组为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
运行结果示例:
```
请输入10个整数:
10 8 6 4 2 9 7 5 3 1
排序后的数组为:
1 2 3 4 5 6 7 8 9 10
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)