代码简化和连接

时间:2017-10-24 06:26:45

标签: java

当被要求回答以下问题时:假设公司的股票点在纽约股票的特定模式中波动 交换。它在给定时期的前半部分每天增加50点并减少25点 下半场得分。如果给定时期内的天数是奇数,则前半部分被认为是a 一天多于下半场。 编写一个Java程序,首先要求用户输入给定时间段内的天数 第一天分享积分。输入的天数不得少于10天 如果不是这样,则必须提示用户再次输入输入,并显示以下消息: “天数不符合要求的标准,请再次输入”。该程序应该输出 显示当天的日期和分享点的表格。

我写了代码:

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)。第二个小问题是我想循环我在代码中注释它的代码,以便它返回到开头 - 但我不知道该怎么做。最后,我最大的问题是我想使用循环语句简化这段代码,但还是无法成功完成。真的是最后一部分是一个延伸,如果我可以得到关于连接问题和循环问题的帮助那么我应该是O.K.并将永远感激。只是我的代码将更难以进行故障排除和阅读。

3 个答案:

答案 0 :(得分:2)

  1. 如果您尝试添加带有数字的字符串,Java将首先将数字更改为字符串,然后将它们全部连接起来。因此,您获得"1 10025"System.out.print(1 + " " + sharePoints + 50);

    因此,您需要将其更改为

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

    然后它将首先计算数学,然后将其更改为String for concat。

  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);
    }
    

答案 1 :(得分:1)

与Alex略有不同的代码。 用户输入循环和算术评估问题由他解决 这是计算共享点的循环。

for(int i = 1; i <= numberofDays; i++)
{
    if(numberofDays % 2 == 0) 
        if(i <= numberofDays/2) {
            sharePoints = sharePoints + 50;
            System.out.println("Day : " + i + " : Share Points : " + sharePoints);
        }
        else {
            sharePoints = sharePoints - 25;
            System.out.println("Day : " + i + " : Share Points : " + sharePoints);
}
else {
    if(i <= numberofDays/2 + 1) {
        sharePoints = sharePoints + 50;
        System.out.println("Day : " + i + " : Share Points : " + sharePoints);
    }
    else {
    sharePoints = sharePoints - 25;
    System.out.println("Day : " + i + " : Share Points : " + sharePoints);
}
}
}

答案 2 :(得分:0)

使用java 8流和lambda的理想方案。