如何在没有for和if语句的情况下编写代码

Happydoodle Pa:

我试图在没有条件语句和循环的情况下重写我的代码。我的代码是基于这样的指令编写的,但是没有循环和条件语句

一个将输入作为开始月份和日期至结束月份和日期以及

计算总价。

假定输入始终正确。

输入范围为同年1月1日至12月31日

1月1/1是星期一。

输入开始日期始终为星期一

偶数月包含31天,奇数月包含30天

价格平日-> $ 2

星期六-> $ 3

周日-> $ 5

如果客户预订超过50天,价格将稳定在1美元


 public class test {
    int startMonth;
    int startDay;
    int endMonth;
    int endDay;
    int totalDate;

    public test (int startMonth, int startDay, int endMonth, int endDay) {
        this.endMonth = endMonth;
        this.endDay = endDay;
        this.startMonth = startMonth;
        this.startDay = startDay;
    }
    public int getPrice() {
        getTotalDate();
        int price = 0;
        int discount = totalDate > 50 ? totalDate - 50 : 0;
        System.out.println("discount " + discount);
        totalDate = totalDate > 50 ? 50 : totalDate % 50;
        System.out.println("totalData : " + totalDate);
        int sunDay = totalDate/7;
        int satDay = totalDate/7 + (totalDate%7)/6;
        int weekDay = totalDate - sunDay - satDay;
        price+= sunDay*5 + satDay*3 + weekDay*2;
        return price + discount;

    }
    public int getTotalDate() {
        int gapOfMonth = endMonth - startMonth;
        totalDate = gapOfMonth*30 + (gapOfMonth +1)/2 + (endDay - startDay);
        return totalDate;
    }

    public static void main (String[] args) {
        test t = new test(1,1,2,30);
        System.out.println("test");
        System.out.println(t.getPrice());
    }
}

Martheen:

无需三元就可以完成它

public int getPrice(int totalDay) {
    int totalPrice = 0;
    int difference = totalDay-50;
    //from https://stackoverflow.com/a/2707438/529282
    int absDifference = difference*(1-2*((3*difference)/(3*difference+1)));

    //this essentially gives the minimum value between totalDay and 50
    int before50 = (totalDay+50-absDifference)/2;

    int after50 = totalDay-before50;

    totalPrice += after50;

    //the before 50 is where the complex calculation is needed
    int before50 = totalDay - after50;

    //first, the base price for weekday
    totalPrice += before50 * 2;

    //then we add the whole week difference (sat+sun price - weekday price)
    totalPrice += (before50 / 7) * 4;

    //the we add the stray saturday if any
    totalPrice += (before50 % 7) / 6;

    return totalPrice;
}

public int getTotalDate() {
    int totalDate = 0;
    //add month difference
    totalDate += 30 * (endMonth - startMonth);

    //add day difference
    totalDate += (endDay - startDay);

    //add the extra from having 31 days every two months
    totalDate += (endMonth - startMonth) / 2;

    //if the month start from even months and the end month is different, 
    //add another day since it ends with 31
    //the trick here, if startMonth == endMonth, startMonth/endMonth = 1,
    //so 1-1 is 0, nothing get added
    //while if startMonth<endMonth, startMonth/endMont = 0, so 1-0 is 1
    totalDate += ((startMonth + 1) % 2) * (1 - startMonth / endMonth);

    return totalDate;

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在没有重复代码的情况下编写或语句(VBA)

在没有if / else的情况下如何编写此语句?

如何在没有一堆if else语句的情况下编写此函数?

测试驱动的开发-如何在没有实现代码的情况下编写测试

如何在没有for循环的情况下编写此matlab代码

如何在没有“需要”注释的情况下编写代码以提高可读性?

如何在没有foreach循环的情况下编写代码?

如何在没有一行 for 循环的情况下编写此 Python 代码?

如何在没有 Matlab 内置函数的情况下编写 3D 直方图代码?

此代码如何在没有任何打印语句的情况下打印Hello World

此代码如何在没有任何循环语句或“ goto”或递归的情况下循环?

如何在没有if语句的情况下做出决定

如何在没有重复代码的情况下实现“ const”和“非const”重载?

如何在没有项目编写器的情况下编写春季批处理步骤

在没有jQuery的情况下,如何用Javascript编写此代码?

如何使用 bootstrap 5 在没有 jQuery 的情况下编写 JavaScript 代码?

如何在没有 ORM 的情况下用 python 编写 SQL?

如何在没有“块”重复甚至 SASS 支持的情况下编写 BEM CSS?

Java 8如何在没有Lambda的情况下编写函数?

如何在没有PHP框架的情况下以MVC模式编写控制器?

如何在没有JOIN的情况下编写SQL查询

如何在没有插件的情况下编写多选输入标签

如何在没有 vsnprintf 的情况下编写 UART 函数

如何在没有所有这些级联错误的圣诞树的情况下编写干净的代码?

Swift 3:如何在没有多个if语句的情况下分配不同选项类型的变量?

如何在没有yield语句的情况下实现Iterator模式(IEnumerator <T>)

如何在没有打印语句的情况下扫描多行

如何在没有任何循环语句的情况下获取数组中多个文件的值

该函数如何在没有return语句的情况下返回某些内容?