gpt4 book ai didi

java - 代码简化和串联

转载 作者:行者123 更新时间:2023-12-01 20:09:25 24 4
gpt4 key购买 nike

当被要求回答以下问题时:假设一家公司的股价在纽约股票中以特定模式波动交换。上半段每天增加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)。第二个小问题是我想循环我在代码中注释过的代码,以便它返回到开头 - 但我不知道该怎么做。最后,我最大的问题是我想使用循环语句来简化此代码,但尚未成功地做到这一点。实际上,最后一部分是一个延伸,如果我能在串联问题和循环问题上获得帮助,那么我应该没问题。并将永远感激不已。只是我的代码将更难以排除故障和阅读。

最佳答案

  1. 如果尝试将字符串与数字相加,Java 会首先将数字更改为字符串,然后将它们全部连接起来。因此,您会在 System.out.print(1 + ""+ sharePoints + 50); 行中得到 "1 10025"

    因此,需要将其更改为

    System.out.print(1 + " " + (sharePoints + 50));

    然后它会先计算数学,然后将其更改为字符串进行连接。

  2. 要在 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天时会要求用户重新输入。

  3. 要使用循环计算不同日期的份额,您可以使用循环动态添加它。在前半天的每次迭代中向 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com