首页 > 解决方案 > 方法中变量的可见性

问题描述

我正在开发一个学生管理程序,该程序在输入姓名后列出学生/指定学生的姓名(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 中。我的导师指出这与:

方法中变量的可见性

如果有任何问题,请告诉我。谢谢你的帮助。

标签: javavariablesdebuggingmethods

解决方案


很好,你正在学习 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");
            }
        }

    }
}

推荐阅读