这是我当前的代码:

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

if转java_java-从if-else转换为方法相关推荐

  1. 如何在Java 8中将Lambda表达式转换为方法引用?

    如果您使用Java 8进行编码,那么您会知道使用方法引用代替lambda表达式会使您的代码更具可读性,因此建议尽可能使用方法引用替换lambda表达式,但是,最大的问题是,您如何查找是否可以用方法引用 ...

  2. amr转换成mp3 java_java将amr文件转换为MP3格式(windowslinux均可使用,亲测)

    使用场景: 业务中需要使用微信语音接口,由于微信上传语音只保存3天,所以需要将语音文件下载到服务器. 但是amr格式文件,前端无法识别,需要将其转换为mp3格式. 装换方法如下: 1.需安装ffmpe ...

  3. date格式化输出 24小时 java_java - 将日期时间转换为24小时表单

    java - 将日期时间转换为24小时表单 我从服务器获得的时间就像Jul 27, 2011 8:35:29 AM. 我想将其转换为2011-07-27 08:35:29. 我还希望转换时间为24小时 ...

  4. getparameter java_java中getAttribute与getParameter方法的区别

    知识点1:getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为object对象类型 例:re ...

  5. getValue()方法 java_java.util.zip.CRC32.getValue()方法示例

    java.util.zip.CRC32.getValue()方法返回校验和值. 声明 以下是java.util.zip.CRC32.getValue方法的声明. public long getValu ...

  6. 动态调用类 java_Java动态调用类中方法

    在Java中,调用类的方法有两种方式:对于静态方法可以直接使用类名调用,对于非静态方法必须使用类的对象调用.反射机制提供了比较另类的调用方式,可以根据需要指定要调用的方法,而不必在编程时确定.调用的方 ...

  7. run在java_Java语言start和run方法的区别

    Java语言start和run方法的区别 导语:Thread类定义了一个功能,用于存储线程要运行的代码,该存储功能就是run方法.那么它和start方法有什么不同呢?让我们了解一下吧! 调用start ...

  8. console java_Java Console format()方法与示例

    console java 控制台类format()方法 (Console Class format() method) format() method is available in java.io ...

  9. console java_Java Console writer()方法与示例

    console java 控制台类writer()方法 (Console Class writer() method) writer() method is available in java.io ...

最新文章

  1. linux 发行版 suse opensuse 区别
  2. 自制浮动静态路由实验(简单)
  3. 【数据结构】C++单链表实现多项式加法(直接输入多项式)
  4. 简述使用计算机对会计工作的影响,简述使用计算机对会计工作的影响。
  5. 小米手机首发开箱图赏
  6. win7装postgresql10.4
  7. murmurhash java_浅析ketamahash和murmurhash
  8. 【笔记】解析MP3文件信息
  9. 《算法图解》高清PDF版
  10. 【OpenCV4 官方文档】机器学习概述
  11. 安鸾CMS系列之74CMS
  12. “IndentationError: unindent does not match any outer indentation level“ 错误解决
  13. springcloud微服务学习笔记(四十五):Gateway9527搭建
  14. iOS 一行代码集成微信浮窗功能
  15. ES2015 Module(模块)
  16. css 文本三行显示,超出省略号表示
  17. 薪资待遇#23届#海尔#嵌入式软件
  18. CDH集群执行任务报错:User xxx not found
  19. 新星计划·第三季 | 更好的总结、创作
  20. windows下使用vscode编写运行以及调试C/C++

热门文章

  1. Getting Started with OpenCV
  2. 无线传输系统功率LCC功率补偿系统设计
  3. docker run 挂载卷_如何在构建期间将主机卷挂载到Dockerfile中的Docker容器中
  4. android 汉字转字节,android实现汉字转拼音功能 带多音字识别
  5. oracle手工快照,创建oracle快照的步骤
  6. Java实用教程笔记 泛型与集合框架
  7. GT Transceiver中的重要时钟及其关系(4)CPLL的工作原理介绍
  8. Verilog初级教程(23)Verilog仿真中的显示任务
  9. 【 Vivado 】UCF到XDC之间的转换
  10. 迭代器、生成器、面向过程编程思想