【问题标题】:How do I pass the user inputs to a constructor in a different class?如何将用户输入传递给不同类的构造函数?
【发布时间】:2017-11-09 03:26:45
【问题描述】:

我的教授告诉我,我必须设计一个具有以下两个字段的类“SharePattern”:“numberOfDaysInPeriod”和 “SharePointsOnFirstDay”。

该类应该有一个构造函数来设置这两个字段的值。

在一个单独的课程中,他说要在我的其他课程的主体中获取用户输入。那么构造函数中的内容是什么?

头等舱:

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);
}
}

二等:

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);
        }
    }
}

【问题讨论】:

  • 在 SharePattern 构造函数中将 'input 2' 更改为 'input2'

标签: java class constructor


【解决方案1】:

头等舱

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

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);
    }

}

【讨论】:

  • 按 numberOfDaysInPeriod 编辑 input1 并按 SharePointsOnFirstDay 编辑 input2
猜你喜欢
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多