某人有100000元,每经过一次路口须缴费,当现金大于>50000 每次交5%,<=50000 每次交1000//编程问可以经过多少次路口 ,要求使用while break

//某人有100000元,每经过一次路口须缴费
//当现金大于>50000 每次交5%
//<=50000 每次交1000
//编程问可以经过多少次路口   要求使用while break
//
// 三种情况
// cash>50000
// cash>=1000 && cash<=50000
// cash<1000
// 多分支 if elseif    else
public class hsp147 {
    public static void main(String[] args){
    	double cash = 100000;
    	int count = 0;
    	// while(cash>50000){
    	// 	cash *= 0.95;
    	// 	count++;
    	// }
    	// while(cash>=1000){
    	// 	cash-=1000;
    	// 	count++;
    	// 	if(cash<=0){
    	// 		break;
    	// 	}
    	// }
    	// System.out.println("次数="+count);
        
        while(true){//写成死循环
            if(cash>50000){
                cash *= 0.95;
                count++;
            }
            else if(cash>=1000 && cash<=50000){
                cash-=1000;
                count++;
            }
            else{
                break;
            }

        }
        System.out.println("次数="+count);    

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
const ElectricityWarningChart:React.FC = () => { const style: React.CSSProperties = {marginBottom: '10px', display: 'flex', justifyContent: 'center'} let dateStr: string= dayjs().format('YYYY-MM') const disabledDate = (current: any) => { return current > dayjs(); } const onChange: DatePickerProps['onChange'] = (date, dateString) => { dateStr = dateString }; return ( <> <ProCard className=" " title="电费管理-预警情况" headerBordered bordered extra={<Space direction="vertical"> <DatePicker onChange={onChange} picker="month" disabledDate={disabledDate} defaultValue={dayjs()} /> </Space>} style={{height: "380px", width: "28%"}}> <ProCard> <Row gutter={8}> <Col className="gutter-row" span={12}> <div style={style}> <Statistic title="无需缴费" value={2833} /> </div> </Col> <Col className="gutter-row" span={12}> <div style={style}> <Statistic title="运营商缴费" value={2519} /> </div> </Col> </Row> <Row gutter={8}> <Col className="gutter-row" span={12}> <div style={style}> <Statistic title="暂缓缴费" value={1222} /> </div> </Col> <Col className="gutter-row" span={12}> <div style={style}> <Statistic title="直供电" value={6699} /> </div> </Col> </Row> <Row gutter={8}> <Col className="gutter-row" span={12}> <div style={style}> <Statistic title="转供电当期" value={4080} /> </div> </Col> <Col className="gutter-row" span={12}> <div style={style}> <Statistic title="缴费逾期" value={436} /> </div> </Col> </Row> <Row gutter={8}> <Col className="gutter-row" span={12}> <div style={style}> <Statistic title="总站址数" value={4080} /> </div> </Col> <Col className="gutter-row" span={12}> <div style={style}> <Statistic title="缴费及时率" value={436} /> </div> </Col> </Row> </ProCard> </ProCard> </> ); }; export default ElectricityWarningChart; 这个代码可以如何优化
07-11
这段代码可以进行一些优化,包括代码结构和性能方面的改进。 1. 提取常量和变量:将样式和日期字符串提取为常量或变量,可以提高代码的可读性和维护性。 ```tsx const style: React.CSSProperties = { marginBottom: '10px', display: 'flex', justifyContent: 'center' }; const currentDate = dayjs().format('YYYY-MM'); ``` 2. 使用 useState 代替直接修改变量:使用 useState 声明 dateStr 变量,并使用 setDateStr 函数来更新它。这样可以确保 React 组件的状态正确更新。 ```tsx const [dateStr, setDateStr] = useState(currentDate); const onChange: DatePickerProps['onChange'] = (date, dateString) => { setDateStr(dateString); }; ``` 3. 使用 useMemo 缓存函数:使用 useMemo 缓存 disabledDate 函数,避免在每次渲染时重新创建函数实例。 ```tsx const disabledDate = useMemo(() => (current: any) => { return current > dayjs(); }, []); ``` 4. 提取组件:将相似的代码块提取为独立的组件,以减少重复代码。例如,可以将 `Statistic` 组件提取为一个单独的组件,并通过 props 传递标题和值。 ```tsx interface StatisticProps { title: string; value: number; } const StatisticCard: React.FC<StatisticProps> = ({ title, value }) => { return ( <div style={style}> <Statistic title={title} value={value} /> </div> ); }; ``` 然后在主组件中使用 `StatisticCard` 组件: ```tsx <Row gutter={8}> <Col className="gutter-row" span={12}> <StatisticCard title="无需缴费" value={2833} /> </Col> <Col className="gutter-row" span={12}> <StatisticCard title="运营商缴费" value={2519} /> </Col> </Row> // 其他相似的代码块也可以使用 StatisticCard 组件 ``` 这样可以减少代码冗余并提高可维护性。 以上是对这段代码的一些优化建议。当然,具体的优化方案还取决于你的项目需求和具体情况。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值