java - 方法中变量的可见性
问题描述
我正在开发一个学生管理程序,该程序在输入姓名后列出学生/指定学生的姓名(VN)、姓氏(NN)和学位(Noten)
在这段代码中有一个 java 方法不能访问声明的数组:
import java.util.Scanner;
import java.util.ArrayList;
public class Aufgabe1_Sulyman {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String NN[] = new String[30];
String VN[] = new String[30];
int Noten[] = new int[30];
int anzahl;
int option;
int index;
do {
System.out.println("Die zur Verfügung gestellten Optionen: ");
System.out.println("1) Eingabe Daten aller Schüler");
System.out.println("2)Ausgabe Daten aller Schüler");
System.out.println("3) Eingabe Daten eines Schülers");
System.out.println("4) Ausgabe Daten eines Schülers");
System.out.println("5) Notendurchschnittsberechnung");
System.out.println("6) Suche nach Nachnamen eines Schülers");
System.out.println("7) Beenden");
option = sc.nextInt();
switch(option) {
case 1:
eingabeAlle(VN, NN, Noten);
break;
case 2:
ausgabeAlle(VN, NN, Noten);
break;
case 3:
System.out.println("Die Nummer des Schülers");
index = sc.nextInt();
eingabe(NN, VN, Noten, index);
break;
case 4:
ausgabe(VN, NN, Noten);
break;
case 5:
System.out.println("die Anzahl der Schüler: ");
anzahl = sc.nextInt();
durchschnitt(anzahl, Noten);
break;
case 6:
suche(NN);
break;
}
} while(option != 7);
}
// Die Methoden
public static void eingabeAlle(String[] VN, String[] NN, int[] Noten) {
for (int i = 0; i < VN.length; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("Geben Sie Den Vornamen des Schülers ein: ");
VN[i] = sc.nextLine();
System.out.println("Geben Sie den Nachnamen des Schülers ein: ");
NN[i] = sc.nextLine();
System.out.println("Geben Sie die Note des Schülers ein:");
Noten[i] = sc.nextInt();
// anzahl =+ VN;
// int sum =+ VN[i];
}
}
public static void eingabe(String[] VN, String[] NN, int[] Noten, int i) {
Scanner sc = new Scanner(System.in);
System.out.println("Geben Sie die Daten der Schüler ein:");
System.out.println("Die notwendigen Daten sind: " + "\n" + "Nachnamen");
NN[i] = sc.nextLine();
System.out.println("Vornamen");
VN[i] = sc.nextLine();
System.out.println("Noten");
Noten[i] = sc.nextInt();
}
public static void ausgabeAlle(String[] VN, String[] NN, int[] Noten) {
for (int i = 0; i < NN.length; i++) {
System.out.println("Die Note für den Schüler: " + NN[i]+ " " + VN[i] + "ist: " + Noten[i]);
}
}
public static void ausgabe(String VN, String NN, int Noten) {
System.out.println("Der Schüler" + NN + " "+ VN + " hat die Note" + Noten);
}
public static void durchschnitt(int anzahl,int[] Noten) {
float d = 0.0F;
for (int i = 0; i < Noten.length; i++) {
d += Noten[i];
}
System.out.println("Der Durchschnitt der Noten aller Schüler ist: " + (d/anzahl));
}
public static void suche(String[] NN) {
System.out.println("Der Nachname des Schülers: ");
String name = sc.nextLine();
for (int i = 0; i < NN.length; i++) {
if (name.equalsIgnoreCase(NN[i])) {
System.out.println(name+ " ist vorhanden");
}
}
}
}
我面临的问题在代码行 37 和 84 中。我的导师指出这与:
方法中变量的可见性
如果有任何问题,请告诉我。谢谢你的帮助。
解决方案
很好,你正在学习 Java
请在您的方法@第 87 行中添加新的 Scanner 对象,并且应更改方法 ausgabe 参数
public static void ausgabe(String[] vN, String[] nN, int[] noten) {
System.out.println("Der Schüler" + nN + " " + vN + " hat die Note" + noten);
}
完整代码
import java.util.Scanner;
import java.util.ArrayList;
public class Aufgabe1_Sulyman {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String NN[] = new String[30];
String VN[] = new String[30];
int Noten[] = new int[30];
int anzahl;
int option;
int index;
do {
System.out.println("Die zur Verfügung gestellten Optionen: ");
System.out.println("1) Eingabe Daten aller Schüler");
System.out.println("2)Ausgabe Daten aller Schüler");
System.out.println("3) Eingabe Daten eines Schülers");
System.out.println("4) Ausgabe Daten eines Schülers");
System.out.println("5) Notendurchschnittsberechnung");
System.out.println("6) Suche nach Nachnamen eines Schülers");
System.out.println("7) Beenden");
option = sc.nextInt();
switch (option) {
case 1:
eingabeAlle(VN, NN, Noten);
break;
case 2:
ausgabeAlle(VN, NN, Noten);
break;
case 3:
System.out.println("Die Nummer des Schülers");
index = sc.nextInt();
eingabe(NN, VN, Noten, index);
break;
case 4:
ausgabe(VN, NN, Noten);
break;
case 5:
System.out.println("die Anzahl der Schüler: ");
anzahl = sc.nextInt();
durchschnitt(anzahl, Noten);
break;
case 6:
suche(NN);
break;
}
} while (option != 7);
}
// Die Methoden
public static void eingabeAlle(String[] VN, String[] NN, int[] Noten) {
for (int i = 0; i < VN.length; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("Geben Sie Den Vornamen des Schülers ein: ");
VN[i] = sc.nextLine();
System.out.println("Geben Sie den Nachnamen des Schülers ein: ");
NN[i] = sc.nextLine();
System.out.println("Geben Sie die Note des Schülers ein:");
Noten[i] = sc.nextInt();
// anzahl =+ VN;
// int sum =+ VN[i];
}
}
public static void eingabe(String[] VN, String[] NN, int[] Noten, int i) {
Scanner sc = new Scanner(System.in);
System.out.println("Geben Sie die Daten der Schüler ein:");
System.out.println("Die notwendigen Daten sind: " + "\n" + "Nachnamen");
NN[i] = sc.nextLine();
System.out.println("Vornamen");
VN[i] = sc.nextLine();
System.out.println("Noten");
Noten[i] = sc.nextInt();
}
public static void ausgabeAlle(String[] VN, String[] NN, int[] Noten) {
for (int i = 0; i < NN.length; i++) {
System.out.println("Die Note für den Schüler: " + NN[i] + " " + VN[i] + "ist: " + Noten[i]);
}
}
public static void ausgabe(String[] vN, String[] nN, int[] noten) {
System.out.println("Der Schüler" + nN + " " + vN + " hat die Note" + noten);
}
public static void durchschnitt(int anzahl, int[] Noten) {
float d = 0.0F;
for (int i = 0; i < Noten.length; i++) {
d += Noten[i];
}
System.out.println("Der Durchschnitt der Noten aller Schüler ist: " + (d / anzahl));
}
public static void suche(String[] NN) {
System.out.println("Der Nachname des Schülers: ");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for (int i = 0; i < NN.length; i++) {
if (name.equalsIgnoreCase(NN[i])) {
System.out.println(name + " ist vorhanden");
}
}
}
}
推荐阅读
- reactjs - React JS 中的 API 调用
- scala - Sbt 插件在其他任务之前/之后运行任务
- angular - 如何在引导模式中将焦点设置为材料日期选择器?
- vue.js - 如何在整个电子应用程序中访问“进程”?
- spring - Spring MVC Rest API - 没有“com.app.dao.AdminsRepository”类型的合格bean
- java - 登录注册 android 应用程序无法连接到本地服务器(连接被拒绝)
- amazon-s3 - 使用 informatica BDM 在 Amazon s3 中创建文件夹模式
- python - Xcode 更新后,“python3 setup.py build_ext --inplace”中断,因为它使用 libstdc++ 而不是 libc++
- javascript - AngularJS 组件 $onChanges 仅在分配新 val 时才被触发,而不是进行更改
- python - 在python中获取一个加密安全的随机数作为整数