2、编写一个类Zong2.java,使用双重while循环画图 ****** ****** ****** ****** ******4
时间: 2024-03-30 14:31:20 浏览: 95
可以编写一个名为Zong2的Java类,使用双重循环来画图。以下是示例代码:
```java
public class Zong2 {
public static void main(String[] args) {
int row = 5; // 设置行数
int col = 6; // 设置列数
int i = 0;
while (i < row) {
int j = 0;
while (j < col) {
System.out.print("*");
j++;
}
System.out.println();
i++;
}
}
}
```
这段代码会输出一个5行6列的星号图案,如下所示:
```
******
******
******
******
******
```
相关问题
用java 编写一个类Zong1.java,用循环嵌套打印由“*”组成的菱形。具体图像如下: * *** ***** ******* ********* ******* ***** *** *
可以使用Java编写一个名为Zong1的类来打印这个菱形图案。以下是一个示例代码:
```java
public class Zong1 {
public static void main(String[] args) {
int n = 5; // 菱形的行数
// 打印上半部分菱形
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
// 打印下半部分菱形
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
```
运行以上代码,将会输出所需的菱形图案。
编写一个类Zong1.java,建立一个对象数组,存放10个学生的学号、姓名、成绩,统计这10个学生的平均成绩,搜索最高成绩和最低成绩的学生的姓名和学号。
下面是Zong1.java的代码实现:
```java
public class Zong1 {
public static void main(String[] args) {
Student[] students = new Student[10];
students[0] = new Student("001", "张三", 80);
students[1] = new Student("002", "李四", 85);
students[2] = new Student("003", "王五", 90);
students[3] = new Student("004", "赵六", 75);
students[4] = new Student("005", "钱七", 70);
students[5] = new Student("006", "孙八", 95);
students[6] = new Student("007", "周九", 87);
students[7] = new Student("008", "吴十", 92);
students[8] = new Student("009", "郑十一", 78);
students[9] = new Student("010", "冯十二", 83);
double sum = 0;
double avg = 0;
double maxScore = 0;
double minScore = Double.MAX_VALUE;
String maxName = "";
String maxId = "";
String minName = "";
String minId = "";
for (int i = 0; i < students.length; i++) {
sum += students[i].getScore();
if (students[i].getScore() > maxScore) {
maxScore = students[i].getScore();
maxName = students[i].getName();
maxId = students[i].getId();
}
if (students[i].getScore() < minScore) {
minScore = students[i].getScore();
minName = students[i].getName();
minId = students[i].getId();
}
}
avg = sum / students.length;
System.out.println("平均成绩:" + avg);
System.out.println("最高成绩学生:" + maxName + " 学号:" + maxId + " 成绩:" + maxScore);
System.out.println("最低成绩学生:" + minName + " 学号:" + minId + " 成绩:" + minScore);
}
}
class Student {
private String id;
private String name;
private double score;
public Student(String id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
```
输出结果为:
```
平均成绩:83.5
最高成绩学生:孙八 学号:006 成绩:95.0
最低成绩学生:钱七 学号:005 成绩:70.0
```
阅读全文