교수님은 "numberOfDaysInPeriod"및 "SharePointsOnFirstDay"필드가있는 클래스 "SharePattern"을 디자인해야한다고 말했습니다.사용자 입력을 다른 클래스의 생성자에 전달하는 방법은 무엇입니까?
클래스에는이 두 필드의 값을 설정하는 생성자가 있어야합니다.
다른 클래스에서 그는 다른 클래스의 메인에서 사용자 입력을 얻으려고 말했다. 그러면 생성자는 무엇이 될까요?
일등석 :
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);
}
}
2 등석 :
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 생성자에서 'input2'를 'input2'로 변경하십시오. – crammeur