Here is my current code:
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)
}
}
}
}
}
This code compiles and works as I want, however, I no longer want it to be in this format. Instead, I would like for it to contain a method named DisplayStock; the input arguments I want for this method are the number of days in the period and the share points on the first day. The method is used to increase the share points by 50 and decrease the share points by 25 on alternate days in the specified period. The method then displays a table showing the days and the share points on those days. This method doesn’t return anything.
As for the main method, it will first ask the users to enter the number of days in the specified period and the share points on the first day (with input validation, the program should then call the DisplayStock method that outputs the table.
A sample output currently looks as such if the period is ll days and SharePoint are 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
So basically, what I am intending to do is convert the code from if-else statements over to methods to alleviate issues with readability and function. Any help would be appreciated! I will continue to work on this but I do not think I will be able to get as far as I intend.
Aucun commentaire:
Enregistrer un commentaire