库里斯托 2023-03-29 18:21 采纳率: 91.1%
浏览 22
已结题

第十三行:怎么让Student类型的数组stu1调用下面的Student方法

package zyntm;
import java.util.ArrayList;
import java.util.Scanner;
public class Users {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> stu = new ArrayList<String>();
        ArrayList<Student> stu1 = new ArrayList<Student>();
        int i = 0;
        while(true){
            String stuID = sc.next();
            stu.add(stuID);
            stu1.get(i) = new Student(stuID.substring(0, 4),stuID.substring(4, 6),stuID.substring(6, 8),stuID.substring(8, 10));
            i++;
        }
        class Student {
            String year;
            String spe;
            String class1;
            String num;
            Student(String year,String spe,String class1,String num){
                this.year = year;
                this.spe = spe;
                this.class1 = class1;
                this.num = num;
            }
            public String getYear() {
                return year;
            }
            public String getSpe() {
                return spe;
            }
            public String getClass1() {
                return class1;
            }
            public String getNum() {
                return num;
            }
        }
       }
}

第十三行:怎么让Student类型的数组stu1调用下面的Student方法?

  • 写回答

1条回答 默认 最新

  • a5156520 2023-03-29 18:54
    关注

    改为stu1.add()方法, 然后把类Student放到Users类外即可,因为如果Student类放到Users类内,则创建Students类对象之前,需要先创建其外部类Users对象。

    修改如下:

    参考链接:

    
    
    package zyntm;
    import java.util.ArrayList;
    import java.util.Scanner;
    public class Users {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            ArrayList<String> stu = new ArrayList<String>();
            ArrayList<Student> stu1 = new ArrayList<Student>();
            int i = 0;
            while(true){
                String stuID = sc.next();
                stu.add(stuID);
                // 使用Arrrays类的add方法,将创建的每个学生类对象添加到stu1中
                stu1.add(new Student(stuID.substring(0, 4),stuID.substring(4, 6),stuID.substring(6, 8),stuID.substring(8, 10)));
              // 打印根据输入信息创建的学生的相关信息
                System.out.println("输入的信息为:"+stu1.get(i));
                i++;
                
            }
            
            
            
        }
         
    }
        
    // 把学生类Student放到类Users类外
        class Student {
            String year;
            String spe;
            String class1;
            String num;
            Student(String year,String spe,String class1,String num){
                this.year = year;
                this.spe = spe;
                this.class1 = class1;
                this.num = num;
            }
            public String getYear() {
                return year;
            }
            public String getSpe() {
                return spe;
            }
            public String getClass1() {
                return class1;
            }
            public String getNum() {
                return num;
            }
            @Override
    // 用于打印学生类的相关信息
            public String toString() {
                return "学生信息: [year=" + year + ", spe=" + spe + ", class1=" + class1 + ", num=" + num + "]";
            }
            
            
        }
      
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 4月6日
  • 已采纳回答 3月29日
  • 创建了问题 3月29日

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗