为了账号安全,请及时绑定邮箱和手机立即绑定

不是声明;预计

不是声明;预计

千万里不及你 2023-06-21 13:26:18
不是声明;预期的我试图将输入的值放入数组中。但它说的不是声明我是编程新手,我正在尝试自学 javaimport 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的学生的其他信息
查看完整描述

4 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

您不能将 an 分配int给整个数组

studID[] = scan.nextInt();

您需要做的是将其分配给数组的一个元素,例如

studID[0] = scan.nextInt();

或者

studID[i] = scan.nextInt();

i索引在哪里

因为你没有循环或使用多个值,为什么你甚至有数组?


查看完整回答
反对 回复 2023-06-21
?
波斯汪

TA贡献1811条经验 获得超4个赞

给定代码,您不需要数组来存储字符串或整数:


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() 返回一个字符串,而不是字符串数组。如果您需要任何帮助,我们非常乐意为您提供帮助。


查看完整回答
反对 回复 2023-06-21
?
交互式爱情

TA贡献1712条经验 获得超3个赞

你在这里做错了几件事。首先,它应该是这样的。; 预期在 java 中结束语句而不是 .(点)。


int studView = scan.nextInt();

现在,您插入数组元素的逻辑不正确。您必须知道,数组是存储在特定索引处的许多元素的集合。因此,您需要将 scan.nextInt() 中的 elem 存储在特定索引处。为了那个原因,


for(int i=0;i<someLength;i++){

 int choice = scan.nextInt();

  switch(choice){

   case 1:

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

            studID[i] = scan.nextInt(); // This i is important here to store at some particular index

   ......................

....................

    }

  }


查看完整回答
反对 回复 2023-06-21
?
临摹微笑

TA贡献1982条经验 获得超2个赞

除了其他答案之外,如果您计划动态地向其添加整数,那么使用ArrayList可能会更好。它不需要预先确定的大小,并允许您将一个整数添加到数组的末尾。希望这可以帮助。



查看完整回答
反对 回复 2023-06-21
  • 4 回答
  • 0 关注
  • 123 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信