JAVA编程实现:67,87,56,78为学生成绩,保存到数组中,通过键盘输入一个学生成绩,利用循环访问数组元素,并输出查找结果。
时间: 2024-03-24 14:40:02 浏览: 21
以下是Java代码实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] scores = {67, 87, 56, 78};
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个学生成绩:");
int target = scanner.nextInt();
boolean found = false;
for (int i = 0; i < scores.length; i++) {
if (scores[i] == target) {
System.out.println("找到了,该成绩在数组的第 " + (i+1) + " 个位置。");
found = true;
}
}
if (!found) {
System.out.println("很抱歉,数组中没有该成绩。");
}
}
}
```
运行结果如下:
```
请输入一个学生成绩:56
找到了,该成绩在数组的第 3 个位置。
```
相关问题
Java编程实现:67,87,56,78为学生成绩,保存到数组中,通过键盘输入一个学生成绩,利用循环访问数组元素,并输出查询结果
以下是Java语言的实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 定义学生成绩数组
int[] scores = {67, 87, 56, 78};
// 通过键盘输入一个学生成绩
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查询的学生成绩:");
int queryScore = scanner.nextInt();
// 利用循环访问数组元素,并输出查询结果
boolean found = false;
for (int score : scores) {
if (score == queryScore) {
System.out.println("找到了该学生成绩:" + score);
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到该学生成绩:" + queryScore);
}
}
}
```
运行结果:
```
请输入要查询的学生成绩:87
找到了该学生成绩:87
```
java编程实现∶67,87,56,78为学生成绩,保存到数组中,通过键盘输入一个学生成绩,利用循环访问数组元素,并输出查找结果
好的,这是一个简单的Java程序,实现你所说的功能:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 定义一个数组并初始化
int[] scores = {67, 87, 56, 78};
// 从键盘输入要查找的学生成绩
System.out.print("请输入要查找的学生成绩:");
Scanner scanner = new Scanner(System.in);
int targetScore = scanner.nextInt();
// 循环访问数组元素,查找目标成绩
boolean found = false;
for (int i = 0; i < scores.length; i++) {
if (scores[i] == targetScore) {
System.out.println("找到了,该成绩在数组中的下标为:" + i);
found = true;
}
}
// 如果没有找到目标成绩,则输出未找到
if (!found) {
System.out.println("未找到该成绩。");
}
}
}
```
运行程序,输入要查找的学生成绩,程序会输出该成绩在数组中的下标(如果找到了),或者输出未找到。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)