if转java_java-从if-else转换为方法

这是我当前的代码:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值