简体   繁体   English

如何将用户输入传递给其他类中的构造函数?

[英]How do I pass the user inputs to a constructor in a different class?

My professor told me that I have to Design a class "SharePattern" that has the following two fields: "numberOfDaysInPeriod" and "SharePointsOnFirstDay". 我的教授告诉我,我必须设计一个类“ SharePattern”,该类具有以下两个字段:“ numberOfDaysInPeriod”和“ SharePointsOnFirstDay”。

The class should have a constructor to set the values of these two fields. 该类应具有一个构造函数来设置这两个字段的值。

In a separate class he said to get user input in the main of my other class. 在一个单独的班级中,他说要在其他班级的主要班级中获得用户输入。 So what goes into the constructor? 那么,构造函数有什么用呢?

First class: 头等舱:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner more = new Scanner(System.in);
    System.out.print("Number of days in the period: ");
    int input1 = more.nextInt();
    while(input1 < 10 || input1 > 20)
    {
        System.out.println("The number of days that is entered must not be less than 10 and more than 20. The number of days doesn't meet the required criteria, enter it again");
        System.out.print("Number of days in the period: ");
        input1 = more.nextInt();
    }

    System.out.print("Share points on the first day: ");
    int input2 = more.nextInt();
    int half = input1 / 2;
    more.close();


    SharePattern sp = new SharePattern(input1, input2, half);
    sp.findFinalDaySharePoints(input1, input2, half);
}
}

2nd class: 二等班:

package hw4Question2;

public class SharePattern {

    public SharePattern(int input1, int input2, int half)//constructor
    {
    }
    public void findFinalDaySharePoints(int input1, int input2, int half) 
    {
        System.out.println(input2);
        if(input1%2 == 0) {
            for(int i = 1; i <= input1 ; ++i)
            {
                if(i<half)
                {
                    input2 = input2 + 50;
                    System.out.println(input2);
                }
                else if(i>half)
                {
                    input2 = input2 - 25;   
                    System.out.println(input2);
                }
            }
        }
        else
        {
            for(int i = 1; i <= input1 ; ++i)
            {
                if(i<=half)
                {           
                    input2 = input2 + 50;
                    System.out.println(input2);
                }
                else if(i>half)
                {
                    input2 = input2 - 25;
                    System.out.println(input2);
                }
            }
            System.out.println("The final share value is "+input2);
        }
    }
}

First class 头等舱

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner more = new Scanner(System.in);
    System.out.print("Number of days in the period: ");
    int input1 = more.nextInt();
    while(input1 < 10 || input1 > 20)
    {
        System.out.println("The number of days that is entered must not be less than 10 and more than 20. The number of days doesn't meet the required criteria, enter it again");
        System.out.print("Number of days in the period: ");
        input1 = more.nextInt();
    }

    System.out.print("Share points on the first day: ");
    int input2 = more.nextInt();
    int half = input1 / 2;
    more.close();


    SharePattern sp = new SharePattern(input1, input2);
    sp.findFinalDaySharePoints(half);


}

SharePattern.class SharePattern.class

int numberOfDaysInPeriod;
int sharePointsOnFirstDay;

public SharePattern(int input1, int input2) {
    this.numberOfDaysInPeriod = input1;
    this.sharePointsOnFirstDay = input2;
}
public void findFinalDaySharePoints(int half)
{
    System.out.println(sharePointsOnFirstDay);
    if(numberOfDaysInPeriod %2 == 0) {
        for(int i = 1; i <= numberOfDaysInPeriod; ++i)
        {
            if(i<half)
            {
                sharePointsOnFirstDay = sharePointsOnFirstDay + 50;
                System.out.println(sharePointsOnFirstDay);
            }
            else if(i>half)
            {
                sharePointsOnFirstDay = sharePointsOnFirstDay - 25;
                System.out.println(sharePointsOnFirstDay);
            }
        }
    }
    else
    {
        for(int i = 1; i <= numberOfDaysInPeriod; ++i)
        {
            if(i<=half)
            {

                sharePointsOnFirstDay = sharePointsOnFirstDay + 50;
                System.out.println(sharePointsOnFirstDay);
            }
            else if(i>half)
            {

                sharePointsOnFirstDay = sharePointsOnFirstDay - 25;
                System.out.println(sharePointsOnFirstDay);
            }

        }
        System.out.println("The final share value is "+ sharePointsOnFirstDay);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户输入的变量传递给构造函数? - How do I pass in user inputed variables into a constructor? 如何将构造函数传递给构造函数? - How do I pass a constructor to a constructor? 为类构造函数使用两个不同的输入 - Using two different inputs for a class constructor 如何创建用户输入列表? - How do I create a list of user inputs? 将参数从构造函数传递给不同类的构造函数 - Pass argument from a constructor to a constructor of a different class 如果我尝试在另一个公共类中将数组作为参数传递,为什么构造函数将无法在Java中编译? - How come my constructor will not compile in Java if I try and pass an array as an argument in a different public class? JAVA:我如何以不同于创建用户输入的方法使用用户输入? - JAVA: How do i use user inputs in a different method from the one in which it was created? 当涉及聚合类时,如何将用户输入输入到数组列表中? - How do I get user inputs into an array list when there is an aggregate class involved? 如何从一个类调用构造函数到另一个不同类的构造函数? - How Do I Call a Constructor from a Class to Another One in a Different Class? 如何将参数传递给类,以便在构造函数中传递参数时JFrame会移动? - How do I pass a parameter into a class so that when its parameter is passed in the constructor the JFrame would move?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM