从if-else转换为方法

时间:2017-11-09 03:21:52

标签: java if-statement methods

这是我目前的代码:

import java.util.Scanner;//importing scanner 

public class QuestionOne {

    public static void main(String[] args) {
        int numberofDays;//these two lines define variables
        int sharePoints;

        Scanner keyboard = new Scanner(System.in);//activating scanner
        System.out.print("Number of days in the period: ");//asking question

        numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

        System.out.print("Share points on the first day: ");//asking another question
        sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

        while (numberofDays < 10 || numberofDays > 20)//while loop makes sure the conditions stay true
        {
            System.out.println("The number of days doesn’t meet the required criteria, enter it again");
            System.out.print("Number of days in the period: ");
            numberofDays = keyboard.nextInt();

            //above three lines ask for number of days until a value that fits within specification is obtained
        }

        System.out.println("Day " + " Share Points");
        System.out.println(1 + "    " + sharePoints);

        //above two lines print day and share points, as well as the first line of text (as it does not change)

        for (int i = 2; i <= numberofDays; i++) {
            if (numberofDays % 2 == 0)

                if (i <= numberofDays / 2) {
                    sharePoints = sharePoints + 50;
                    System.out.println(i + "    " + sharePoints);
                } else {
                    sharePoints = sharePoints - 25;
                    System.out.println(i + "    " + sharePoints);
                } else {
                    if (i <= numberofDays / 2 + 1) {
                        sharePoints = sharePoints + 50;
                        System.out.println(i + "    " + sharePoints);
                } else {
                    sharePoints = sharePoints - 25;
                    System.out.println(i + "    " + sharePoints);

                    // above nested if else statements essentially calculate and concatenate the variables to obtain an answer that is then printed and repeated until the number of days is reached (starting from day two)
                }
            }
        }
    }
}

此代码按我的意愿编译和工作,但是,我不再希望它采用这种格式。相反,我希望它包含一个名为DisplayStock的方法;我想要这个方法的输入参数是 期间的天数和第一天的分享点数。该方法用于将共享点数增加50,并在指定时间段内隔日将共享点减少25。那个方法呢 显示一个表格,显示当天的日期和分享点。这种方法不会返回任何内容。

对于主要方法,它将首先要求用户输入指定时间段内的天数和第一天的共享点(通过输入验证,程序应该调用输出表格的DisplayStock方法)

如果句点为ll天且SharePoint为550,则示例输出当前看起来是这样的:

Day Share Points
1 550
2 600
3 575
4 625
5 600
6 650
7 625
8 675
9 650
10 700
11 675

基本上,我打算做的是将if-else语句中的代码转换为方法,以减轻可读性和功能方面的问题。任何帮助,将不胜感激!我将继续努力,但我认为我无法达到我想要的程度。

3 个答案:

答案 0 :(得分:2)

你只想将代码分成两个或几个方法 基本将是这样的:

  public static void main(String[] args) {
    int numberofDays;//these two lines define variables
    int sharePoints;

    Scanner keyboard = new Scanner(System.in);//activating scanner
    System.out.print("Number of days in the period: ");//asking question

    numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

    System.out.print("Share points on the first day: ");//asking another question
    sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

    numberofDays = validator(numberofDays,keyboard); // Validates Keyboard input

    //above two lines print day and share points, as well as the first line of text (as it does not change)
    outPutTablePrinter(numberofDays,sharePoints);

}

private static void outPutTablePrinter(int numberOfDays,int sharePoints){
    System.out.println("Day " + " Share Points");
    System.out.println(1 + "    " + sharePoints);
    for (int i = 2; i <= numberOfDays; i++) {
        if (numberOfDays % 2 == 0)
            if (i <= numberOfDays / 2) {
                sharePoints = sharePoints + 50;
                System.out.println(i + "    " + sharePoints);
            } else {
                sharePoints = sharePoints - 25;
                System.out.println(i + "    " + sharePoints);
            } else {
            if (i <= numberOfDays / 2 + 1) {
                sharePoints = sharePoints + 50;
                System.out.println(i + "    " + sharePoints);
            } else {
                sharePoints = sharePoints - 25;
                System.out.println(i + "    " + sharePoints);
                // above nested if else statements essentially calculate and concatenate the variables to obtain an answer that is then printed and repeated until the number of days is reached (starting from day two)
            }
        }
    }
}

private static int validator(int numberOfDays,Scanner keyboard){
    while (numberOfDays < 10 || numberOfDays > 20)//while loop makes sure the conditions stay true
    {
        System.out.println("The number of days doesn’t meet the required criteria, enter it again");
        System.out.print("Number of days in the period: ");
        numberOfDays = keyboard.nextInt();
    }
    return numberOfDays;
}

答案 1 :(得分:1)

我重构了你的代码。首先,我更喜欢printf,通过在循环中递增2,可以大大简化您的逻辑。你需要检查是否有一个索引+ 1,它可以在奇数天内正常工作,但你可以做到

System.out.printf("Day\tShare Points%n");
for (int i = 0; i < numberofDays; i += 2) {
    System.out.printf("%-3d\t%d%n", i + 1, sharePoints);
    sharePoints += 50;
    if (i + 1 < numberofDays) {
        System.out.printf("%-3d\t%d%n", i + 2, sharePoints);
        sharePoints -= 25;
    }
}

我得到了

Day Share Points
1   550
2   600
3   575
4   625
5   600
6   650
7   625
8   675
9   650
10  700
11  675

答案 2 :(得分:1)

使用以下代码:

import java.util.Scanner;//importing scanner 

public class QuestionOne{
      static int numberofDays;//these two lines define variables
      static int sharePoints;

  public static void main(String[]args){


      Scanner keyboard = new Scanner (System.in);//activating scanner

      System.out.print("Number of days in the period: ");//asking question

      numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

      System.out.print("Share points on the first day: ");//asking another question

      sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

    while (numberofDays < 10 || numberofDays > 20)//while loop makes sure the conditions stay true
     {
        System.out.println("The number of days doesn’t meet the required criteria, enter it again");

        System.out.print("Number of days in the period: ");

        numberofDays = keyboard.nextInt();

        //above three lines ask for number of days until a value that fits within specification is obtained
      } 

    DisplayStock ();

  }
  public static void DisplayStock (){
      System.out.println("Day " + " Share Points");
      System.out.println(1 + "    " + sharePoints);

      //above two lines print day and share points, as well as the first line of text (as it does not change)

      for(int i = 2; i <= numberofDays; i++)
      {
        if(numberofDays % 2 == 0)

          if(i <= numberofDays/2)
          {
          sharePoints = sharePoints + 50;  
          System.out.println(i + "    " + sharePoints);
        }
        else
        {
          sharePoints = sharePoints - 25;
          System.out.println(i + "    " + sharePoints);
        }
        else
        {
          if(i <= numberofDays/2 + 1)
          {
            sharePoints = sharePoints + 50;
            System.out.println(i + "    " + sharePoints);
          }
          else
          {
            sharePoints = sharePoints - 25;
            System.out.println(i + "    " + sharePoints);

            //above nested if else statements essentially calculate and concatenate the variables to obtain an answer that is then printed and repeated until the number of days is reached (starting from day two)
            }
        }
      }
  }
}