从键盘输入一个班(全班最多不超过30人)学生某门课的成绩,当输入成绩为负值时, 输入结束,
时间: 2024-06-14 20:04:05 浏览: 203
以下是两种实现从键盘输入学生某门课成绩的方法:
1. Java实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] scores = new int[30];
int count = 0;
System.out.println("请输入学生的成绩(输入负值结束):");
while (true) {
int score = scanner.nextInt();
if (score < 0) {
break;
}
scores[count] = score;
count++;
if (count >= 30) {
break;
}
}
System.out.println("录入的成绩为:");
for (int i = 0; i < count; i++) {
System.out.println(scores[i]);
}
}
}
```
2. C语言实现:
```c
#include <stdio.h>
int main() {
int scores[30];
int count = 0;
printf("请输入学生的成绩(输入负值结束):\n");
while (1) {
int score;
scanf("%d", &score);
if (score < 0) {
break;
}
scores[count] = score;
count++;
if (count >= 30) {
break;
}
}
printf("录入的成绩为:\n");
for (int i = 0; i < count; i++) {
printf("%d\n", scores[i]);
}
return 0;
}
```
阅读全文