- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当被要求回答以下问题时:假设一家公司的股价在纽约股票中以特定模式波动交换。上半段每天增加50点,每天减少25点下半场得分。如果给定期间的天数为奇数,则前半部分被视为比下半场多了一天。编写一个 Java 程序,首先要求用户输入给定时间段内的天数以及第一天分享积分。输入的天数不得少于10天大于 20。如果不是这样,则必须提示用户再次输入,并显示以下消息:“天数不符合要求,请重新输入”。程序应该输出显示日期和这些日期的分享点的表格。
我写的代码:
import java.util.Scanner;
public class QuestionOne
{
public static void main(String[]args)
{
int numberofDays;
int sharePoints;
Scanner keyboard = new Scanner (System.in);
System.out.print("Number of days in the period: ");
numberofDays = keyboard.nextInt();
System.out.print("Share points on the first day: ");
sharePoints = keyboard.nextInt();
if(numberofDays < 10 || numberofDays > 20)
{
System.out.println("The number of days doesn’t meet the required criteria, enter it again");
// needs to loop to beginning to make program carry out proper function//
}
else
{
System.out.println("Day " + " Share Points");
switch(numberofDays)
{
case 1:
System.out.println(1 + " " + sharePoints + 50);
break;
case 2:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 25);
break;
case 3:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 75);
break;
case 4:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 75);
System.out.println(4 + " " + sharePoints + 50);
break;
case 5:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 75);
System.out.println(5 + " " + sharePoints + 50);
break;
case 6:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 125);
System.out.println(5 + " " + sharePoints + 100);
System.out.println(6 + " " + sharePoints + 75);
break;
case 7:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 175);
System.out.println(6 + " " + sharePoints + 150);
System.out.println(7 + " " + sharePoints + 125);
break;
case 8:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 175);
System.out.println(6 + " " + sharePoints + 150);
System.out.println(7 + " " + sharePoints + 125);
System.out.println(8 + " " + sharePoints + 100);
break;
case 9:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 225);
System.out.println(7 + " " + sharePoints + 200);
System.out.println(8 + " " + sharePoints + 175);
System.out.println(9 + " " + sharePoints + 150);
break;
case 10:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 225);
System.out.println(7 + " " + sharePoints + 200);
System.out.println(8 + " " + sharePoints + 175);
System.out.println(9 + " " + sharePoints + 150);
System.out.println(10 + " " + sharePoints + 125);
break;
case 11:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 275);
System.out.println(8 + " " + sharePoints + 250);
System.out.println(9 + " " + sharePoints + 225);
System.out.println(10 + " " + sharePoints + 200);
System.out.println(11 + " " + sharePoints + 175);
break;
case 12:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 275);
System.out.println(8 + " " + sharePoints + 250);
System.out.println(9 + " " + sharePoints + 225);
System.out.println(10 + " " + sharePoints + 200);
System.out.println(11 + " " + sharePoints + 175);
System.out.println(12 + " " + sharePoints + 150);
break;
case 13:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 350);
System.out.println(8 + " " + sharePoints + 325);
System.out.println(9 + " " + sharePoints + 300);
System.out.println(10 + " " + sharePoints + 275);
System.out.println(11 + " " + sharePoints + 250);
System.out.println(12 + " " + sharePoints + 225);
System.out.println(13 + " " + sharePoints + 200);
break;
case 14:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 350);
System.out.println(8 + " " + sharePoints + 325);
System.out.println(9 + " " + sharePoints + 300);
System.out.println(10 + " " + sharePoints + 275);
System.out.println(11 + " " + sharePoints + 250);
System.out.println(12 + " " + sharePoints + 225);
System.out.println(13 + " " + sharePoints + 200);
System.out.println(14 + " " + sharePoints + 175);
break;
case 15:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 350);
System.out.println(8 + " " + sharePoints + 400);
System.out.println(9 + " " + sharePoints + 375);
System.out.println(10 + " " + sharePoints + 350);
System.out.println(11 + " " + sharePoints + 325);
System.out.println(12 + " " + sharePoints + 300);
System.out.println(13 + " " + sharePoints + 275);
System.out.println(14 + " " + sharePoints + 250);
System.out.println(15 + " " + sharePoints + 225);
break;
case 16:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 350);
System.out.println(8 + " " + sharePoints + 400);
System.out.println(9 + " " + sharePoints + 375);
System.out.println(10 + " " + sharePoints + 350);
System.out.println(11 + " " + sharePoints + 325);
System.out.println(12 + " " + sharePoints + 300);
System.out.println(13 + " " + sharePoints + 275);
System.out.println(14 + " " + sharePoints + 250);
System.out.println(15 + " " + sharePoints + 225);
System.out.println(16 + " " + sharePoints + 200);
break;
case 17:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 350);
System.out.println(8 + " " + sharePoints + 400);
System.out.println(9 + " " + sharePoints + 450);
System.out.println(10 + " " + sharePoints + 425);
System.out.println(11 + " " + sharePoints + 400);
System.out.println(12 + " " + sharePoints + 375);
System.out.println(13 + " " + sharePoints + 350);
System.out.println(14 + " " + sharePoints + 325);
System.out.println(15 + " " + sharePoints + 300);
System.out.println(16 + " " + sharePoints + 275);
System.out.println(17 + " " + sharePoints + 250);
break;
case 18:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 350);
System.out.println(8 + " " + sharePoints + 400);
System.out.println(9 + " " + sharePoints + 450);
System.out.println(10 + " " + sharePoints + 425);
System.out.println(11 + " " + sharePoints + 400);
System.out.println(12 + " " + sharePoints + 375);
System.out.println(13 + " " + sharePoints + 350);
System.out.println(14 + " " + sharePoints + 325);
System.out.println(15 + " " + sharePoints + 300);
System.out.println(16 + " " + sharePoints + 275);
System.out.println(17 + " " + sharePoints + 250);
System.out.println(18 + " " + sharePoints + 225);
break;
case 19:
System.out.println(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 350);
System.out.println(8 + " " + sharePoints + 400);
System.out.println(9 + " " + sharePoints + 450);
System.out.println(10 + " " + sharePoints + 500);
System.out.println(11 + " " + sharePoints + 475);
System.out.println(12 + " " + sharePoints + 450);
System.out.println(13 + " " + sharePoints + 425);
System.out.println(14 + " " + sharePoints + 400);
System.out.println(15 + " " + sharePoints + 375);
System.out.println(16 + " " + sharePoints + 350);
System.out.println(17 + " " + sharePoints + 325);
System.out.println(18 + " " + sharePoints + 300);
System.out.println(19 + " " + sharePoints + 275);
break;
case 20:
System.out.print(1 + " " + sharePoints + 50);
System.out.println(2 + " " + sharePoints + 100);
System.out.println(3 + " " + sharePoints + 150);
System.out.println(4 + " " + sharePoints + 200);
System.out.println(5 + " " + sharePoints + 250);
System.out.println(6 + " " + sharePoints + 300);
System.out.println(7 + " " + sharePoints + 375);
System.out.println(8 + " " + sharePoints + 400);
System.out.println(9 + " " + sharePoints + 450);
System.out.println(10 + " " + sharePoints + 500);
System.out.println(11 + " " + sharePoints + 475);
System.out.println(12 + " " + sharePoints + 450);
System.out.println(13 + " " + sharePoints + 425);
System.out.println(14 + " " + sharePoints + 400);
System.out.println(15 + " " + sharePoints + 375);
System.out.println(16 + " " + sharePoints + 350);
System.out.println(17 + " " + sharePoints + 325);
System.out.println(18 + " " + sharePoints + 300);
System.out.println(19 + " " + sharePoints + 275);
System.out.println(20 + " " + sharePoints + 250);
break;
}
}
}
}
但是,我遇到了一个大问题和两个小问题;第一个较小的问题是我的答案是输入的数字加上我想要添加的数字(所以如果你输入 100,它加了 25,你应该得到 125 - 但我得到 10025)。第二个小问题是我想循环我在代码中注释过的代码,以便它返回到开头 - 但我不知道该怎么做。最后,我最大的问题是我想使用循环语句来简化此代码,但尚未成功地做到这一点。实际上,最后一部分是一个延伸,如果我能在串联问题和循环问题上获得帮助,那么我应该没问题。并将永远感激不已。只是我的代码将更难以排除故障和阅读。
最佳答案
如果尝试将字符串与数字相加,Java 会首先将数字更改为字符串,然后将它们全部连接起来。因此,您会在 System.out.print(1 + ""+ sharePoints + 50);
行中得到 "1 10025"
。
因此,需要将其更改为
System.out.print(1 + " " + (sharePoints + 50));
然后它会先计算数学,然后将其更改为字符串进行连接。
要在 true 时循环某些内容,例如用户输入,您可以使用以下代码段。
Scanner keyboard = new Scanner (System.in);
System.out.print("Number of days in the period: ");
numberofDays = keyboard.nextInt();
while (numberofDays < 10 || numberofDays > 20)
{
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();
}
天数小于10天和大于20天时会要求用户重新输入。
要使用循环计算不同日期的份额,您可以使用循环动态添加它。在前半天的每次迭代中向 sharePoints 添加 50,并在后半天的 sharePoints 中减去 25,如下所示。
int increaseDays = (int) (numberofDays + 0.5) / 2;
int i;
for (i = 1; i <= increaseDays; i++)
{
sharePoints += 50;
System.out.println(i + " " + sharePoints);
}
for (; i <= numberofDays; i++)
{
sharePoints -= 25;
System.out.println(i + " " + sharePoints);
}
关于java - 代码简化和串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46903541/
hello1 hello2 hello3 hello4 hello5 hello6
有没有更简短的写法: (apply f (cons a (cons b (cons c d)))) ? 谢谢! (我正在编写一些调用其他函数的辅助函数,这种“模式”似乎经常出现
.NET团队北京时间2024年5月22日已正式发布.NET Aspire ,在博客文章里做了详细的介绍:.NET Aspire 正式发布:简化 .NET 云原生开发 - .NET 博客 (micros
在this dbfiddle demo我有一个 DELETE FROM...WHERE 最后像这样: ...... DELETE FROM data_table WHERE
我有几个 if 语句,如下面的一个。我假设这是一种非常糟糕/长期的编码方式,但不确定我应该做些什么不同的事情。有人有什么建议吗? 谢谢 For a = 1 To Leagues If a =
有什么类似的战术simpl为 Program Fixpoint ? 特别是,如何证明以下无关紧要的陈述? Program Fixpoint bla (n:nat) {measure n} := mat
我使用此代码来跟踪表单上是否有任何更改: $(document).on('input', '.track', function() { var form = $(this); }); 由于这不
我有以下函数,我想用 for 循环来简化它,但不知道该怎么做。任何帮助都感激不尽。基本上,如果字段值为 0 或 null,则我的总值(字段)应为 0,否则,如果字段值从 1 到 1000,则总值变为
我正在尝试对时间字符串执行非常简单的解析 data Time = Time Int Int Int String -- example input: 07:00:00AM timeParser ::
为了使我的代码更具可读性和更简单,我对这段代码绞尽脑汁: var refresh = setInterval(datumTijd, 1000); function datumTijd() { do
这个问题已经有答案了: Check if a variable is in an ad-hoc list of values (8 个回答) 已关闭 9 年前。 只是一个基本的if声明,试图使其更简单
我有一个这样的 if 语句 int val = 1; if (val == 0 || val == 1 || val == 2 || ...); 有没有更简单的方法?例如: int val = 1;
我有一个程序,其中有一些 if 语句,与我将要向您展示的程序类似。我想知道你们是否可以帮助我以任何方式简化这个方程。我之所以问这个问题,是因为在我的 Notepad++ 中,它持续了 443 列,如果
是否可以简化这个 if 语句? 如果是,答案是什么? if (type) { if(NdotL >= 0.0) { color
我有一个包含亚马逊大河的 shapefile。仅 shapefile 就有 37.9 MB,连同属性表高达 42.1 MB。我正在生成所有巴西亚马逊的 PNG 图像,每个 1260x940 像素,sh
System.out.printf("%7s", "a"); System.out.printf("%7s", "b"); System.out.printf("%7s", "c"); S
假设我们有客户端-服务器应用程序,由一个 makefile 编译。服务器使用 libtask 为并行客户端提供服务。客户端使用 ncurses 来处理某些图形。目录树如下所示: ./ --bin/ -
我在 Mono 密码转换的重新实现中找到了这段代码。 我没有修改或简化任何东西 - 这就是它的实际运行方式(有评论如//Dispose unmanaged objects,但实际上什么也没做)。 现在
我需要一些帮助来简化这个包含数百行的庞大代码,但我真的不知道该怎么做。代码看起来真的很乱,我需要的是返回具有预定义文本颜色的模型。有什么简单的方法吗? 我必须多解释一点:- 有一个包含许多型号的手机列
这里有一些代码可以正常工作,但我认为可以简化/缩短。它基本上是点击一个列表项,获取它的 ID,然后根据 ID 显示/隐藏/删除元素。 关于如何使用函数或循环来简化它的建议? $("#btn_remov
我是一名优秀的程序员,十分优秀!