当前位置: 首页 > 知识库问答 >
问题:

如何将数组长度设置为变量Java

仉伟兆
2023-03-14

所以我做了一个小程序,在把字符串数组的长度设置成用户指定的大小时遇到了麻烦,你是怎么把用户输入的值分配给数组长度的呢?我有一个用于用户输入的主类,它传递给一个演示类来存储和运行一些不同的计算等。mac_attendee_limit的输入将是数组的大小

主类

public class ProgMgmtSys {

private static Scanner sc = new Scanner(System.in);

public static void displayMenu() {
    System.out.println("\nMoolort Heritage Railway Demonstration Booking System\n");
    System.out.println("Menu");
    System.out.println("A: Add Demonstration");
    System.out.println("B: Add Attendee");
    System.out.println("C: Print Demonstration");
    System.out.println("D: Print Attendee");
    System.out.println("E: Select & Print Cost");
    System.out.println("Q: quit");
    System.out.print("Please enter your selection: ");
}

public static void addNewDemonstration(Demonstration demonstration) {

    String  identifier, title;
    double base_fee;
    int max_attendee_limit, start_time, duration;

    System.out.println("New Demonstration Creater");
    System.out.println("Enter an Identifier");
    identifier = sc.nextLine();
    System.out.println("Enter a Title");
    title = sc.nextLine();
    System.out.println("Enter the Base Fee");
    base_fee = sc.nextDouble();
    System.out.println("Enter the Macimum Attendee Limit");
    max_attendee_limit = sc.nextInt();
    System.out.println("Enter the Start Time");
    start_time = sc.nextInt();
    System.out.println("Enter the Duration");
    duration = sc.nextInt();

    demonstration.newDemonstration(identifier, title, base_fee, max_attendee_limit, start_time, duration);  

}

public static void newAttendeeBooking(Demonstration demonstration) {

    String attendeeName, attendeePhoneNumber, membershipType;

    System.out.println("New Attendee Booking");
    System.out.println("Enter Attendee Name: ");
    attendeeName  = sc.nextLine();
    System.out.println("Enter Attendee Phone Number: ");
    attendeePhoneNumber = sc.nextLine();
    System.out.println("Please enter discount type if applicable [concession, fsrs, arhs, mhr]: ");
    membershipType = sc.nextLine().toLowerCase();

    demonstration.newAttendee(attendeeName, attendeePhoneNumber, membershipType);

}

public static void selectDemonstrationsCost(Demonstration demonstration) {

    String discountType;

    System.out.println("Please Select the type of discount to see the cost for all demonstrations:");
    System.out.print("concession, fsrs, arhs, mhr");
    discountType = sc.nextLine().toLowerCase();

    demonstration.printDemonstrationsCost(discountType);

}


public static void main(String[] args) {
    String choice;
    Demonstration demonstration = new Demonstration();
    do {
        displayMenu();
        choice = sc.nextLine().toUpperCase();
        switch(choice) {
        case "A":
            addNewDemonstration(demonstration);
            break;
        case "B":
            newAttendeeBooking(demonstration);
            break;
        case "C":
            demonstration.printDemonstration();
            break;
        case "D":
            demonstration.printAttendee();
            break;
        case "E":
            selectDemonstrationsCost(demonstration);
            break;

        case "Q":
            System.out.println("Goodbye");
            break;
        default:
            System.out.println("Invalid selection. Please try again.");
        }
    } while (!choice.equals("Q"));      
}

}

演示课

public class Demonstration {

    private String  identifier;
    private String  title;
    private double base_fee;
    private int max_attendee_limit = 0;
    private int start_time;
    private int duration;
    private int newAttendee = 5;

    private String attendeeName[] = new String[max_attendee_limit];
    private String attendeePhoneNumber[] = new String[max_attendee_limit];
    private String membershipType[] = new String[max_attendee_limit];

    //Discount charges for all different societies & concession
    static final double concession_disc = .10; //10% Disc
    static final double fsrs_disc = .20; // 20% Discount
    static final double arhs_disc = .25; // 25% Discount
    static final double mhr_disc = 1.00; // 100% Discount - FREE

    private int i = 0;

    public Demonstration() {

    }

    public void newDemonstration(String identifier, String title, double base_fee, int max_attendee_limit, int start_time, int duration) {

        this.identifier = identifier;
        this.title = title;
        this.base_fee = base_fee;
        this.max_attendee_limit = max_attendee_limit;
        this.start_time = start_time;
        this.duration = duration;   

    }

    public void newAttendee(String attendeeName, String attendeePhoneNumber, String membershipType) {

        if (i < newAttendee) {

            this.attendeeName[i] = attendeeName;
            this.attendeePhoneNumber[i] = attendeePhoneNumber;
            this.membershipType[i] = membershipType;

            i++;
        }       
    }

    public void printDemonstration() {
        System.out.println("Identifier: " + identifier);
        System.out.println("Title: " + title);
        System.out.println("Base Fee: $" + base_fee);
        System.out.println("Maximum Attendee Limit: " + max_attendee_limit);
        System.out.println("Start Time: " + start_time);
        System.out.println("Duration: " + duration);

    }

    public void printAttendee() {

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

            System.out.println("Attendee Name: " + attendeeName[j]);
            System.out.println("Attendee Phone number: " + attendeePhoneNumber[j]);
            System.out.println("Membership Type: $" + membershipType[j]);

        }

    }
}

共有1个答案

宇文育
2023-03-14

我想你需要初始化函数内部的数组。这是因为如果你把它放在全球。当u创建类exemonary exemonary=new exemonary();时,此时将创建全局数组。然后,虽然u更改了max_attendee_limit,但它不会更改数组的大小。

您可以这样做:

public void newDemonstration(String identifier, String title, double base_fee, int max_attendee_limit, int start_time, int duration) {

        this.identifier = identifier;
        this.title = title;
        this.base_fee = base_fee;
        this.max_attendee_limit = max_attendee_limit;
        this.start_time = start_time;
        this.duration = duration;   

        attendeeName = new String[max_attendee_limit];
        attendeePhoneNumber = new String[max_attendee_limit];
        membershipType = new String[max_attendee_limit];
    }
 类似资料:
  • 我将一个字符串设置为activeElement.value,一切正常。现在,我想为activeElement.value设置一个变量,我得到了以下错误代码: 线程“main”中的异常 org.openqa.selenium.WebDriver异常:未定义患者 ID 命令持续时间或超时:66 毫秒 构建信息:版本:“2.45.0”,修订版:“5017cb8”,时间:“2015-02-26 23:59

  • 假设我想要一个变量包含从1到100的数字。我可以这样做: 但是把所有这些数字写下来需要很多时间。有没有办法给这个变量设置一个范围?类似的东西: 这听起来可能是一个非常愚蠢的问题,但我自己还没有弄清楚。提前感谢。

  • 问题内容: 我正在尝试显示媒体存储的多个列,例如艺术家,专辑,标题等。我能够正确获取arraylist中的一个列,并且可以正常工作,但是当我添加另一个列时,所有列都在同一列中数组列表,所以我创建了一个类,并在该类方法中添加了列。但是我无法在custome适配器中声明它,这是我的代码。 活动主体 方法类别 CustomeAdapter 定制XML 如何将方法设置为数组。如何获取任何示例的方法都将有所

  • 问题内容: 我正在使用WAMP。我想从命令提示符下使用php。为此,PATH env变量中的条目是什么? 问题答案: 你需要把具有目录在您安装到您。通常是这样的

  • 问题内容: 我不想每次运行gulp设置环境变量时都键入额外的参数。 我宁愿 通过任务从gulp中 设置环境变量。 什么是实现这一目标的好方法? 问题答案: 像这样使用它:

  • 问题内容: 我想使一个组件占据Container的maximumAvailableHeight。例如,在下面粘贴的代码中,我将根框架定为800,600。我只想设置该框架的高度/宽度(并且我不想尝试对其子像素进行像素化)。如果运行此命令,则会看到UI对齐不良。 首先,我希望面板(位于根框架内)占据框架的100%的高度(在这种情况下,为800px减去用于绘制框架标题的空间)。 其次,在面板内部,我有一