不是声明;预计

不是声明;预期的


我试图将输入的值放入数组中。但它说的不是声明


我是编程新手,我正在尝试自学 java


import java.util.Scanner;


public class MainClass {

    public static void main(String[] args){

        int[] studID = new int[100];

        String[] Lname = new String[100];

        String[] Fname = new String[100];

        String[] studProgram = new String[100];

        int[] studYear = new int[100];



        System.out.println("1. add student");

        System.out.println("2. view student");

        System.out.println("3. edit student");

        System.out.println("4. delete student");

        System.out.print("Please choose: ");

        Scanner scan = new Scanner(System.in);

        int choice = scan.nextInt();


        switch(choice){


            case 1:

                System.out.print("Enter ID: ");

                studID[] = scan.nextInt();


                System.out.print("Enter Last name: ");

                Lname[] = scan.next();


                System.out.print("Enter First name: ");

                Fname[] = scan.next();


                System.out.print("Enter Course: ");

                studProgram[] = scan.next();


                System.out.print("Enter Year: ");

                studYear[] = scan.nextInt();


            case 2:

                 System.out.println("Enter student ID to view: ");

                 int studView = scan.


        }

    } 

}

我期望当我将学生的值放入数组中时,如果我选择查看学生ID,它还会显示依赖学生ID的学生的其他信息


千万里不及你
浏览 127回答 4
4回答

牧羊人nacy

您不能将 an 分配int给整个数组studID[] = scan.nextInt();您需要做的是将其分配给数组的一个元素,例如studID[0] = scan.nextInt();或者studID[i] = scan.nextInt();i索引在哪里但因为你没有循环或使用多个值,为什么你甚至有数组?

波斯汪

给定代码,您不需要数组来存储字符串或整数:int[] studID = new int[100];String[] Lname = new String[100];String[] Fname = new String[100];String[] studProgram = new String[100];int[] studYear = new int[100];只需像这样声明它们:int studID;String Lname;String Fname;String studProgram;int studYear;在这个开关盒中:switch(choice){        case 1:            System.out.print("Enter ID: ");            studID = scan.nextInt();            System.out.print("Enter Last name: ");            Lname = scan.next();            System.out.print("Enter First name: ");            Fname = scan.next();            System.out.print("Enter Course: ");            studProgram = scan.next();            System.out.print("Enter Year: ");            studYear = scan.nextInt();    }nextInt() 方法返回一个整数,而不是一个数组,next() 返回一个字符串,而不是字符串数组。如果您需要任何帮助,我们非常乐意为您提供帮助。

交互式爱情

你在这里做错了几件事。首先,它应该是这样的。; 预期在 java 中结束语句而不是 .(点)。int studView = scan.nextInt();现在,您插入数组元素的逻辑不正确。您必须知道,数组是存储在特定索引处的许多元素的集合。因此,您需要将 scan.nextInt() 中的 elem 存储在特定索引处。为了那个原因,for(int i=0;i<someLength;i++){&nbsp;int choice = scan.nextInt();&nbsp; switch(choice){&nbsp; &nbsp;case 1:&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter ID: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; studID[i] = scan.nextInt(); // This i is important here to store at some particular index&nbsp; &nbsp;..........................................&nbsp; &nbsp; }&nbsp; }

临摹微笑

除了其他答案之外,如果您计划动态地向其添加整数,那么使用ArrayList可能会更好。它不需要预先确定的大小,并允许您将一个整数添加到数组的末尾。希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java