2017-11-09 15 views
0

私の教授は、 "numberOfDaysInPeriod"と "SharePointsOnFirstDay"の2つのフィールドを持つクラス "SharePattern"をデザインする必要があると教えてくれました。ユーザ入力を別のクラスのコンストラクタに渡すにはどうすればよいですか?

クラスには、これら2つのフィールドの値を設定するコンストラクタが必要です。

別のクラスで、彼は私の他のクラスのメインでユーザー入力を得ると言った。だから、コンストラクタには何が入りますか?

ファーストクラス:

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

SharePatternコンストラクタで 'input2'を 'input2'に変更します。 – crammeur

答えて

0

ファーストクラス

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

} 
+0

edit input1 by numberOfDaysInPeriodおよびinput2 by SharePointsOnFirstDay – crammeur

関連する問題