这是我当前的代码:
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语句转换为方法,以减轻可读性和功能问题.任何帮助,将不胜感激!我将继续为此工作,但我认为我将无法达到我的预期.