[英]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.