Loop_while

本文介绍了Java中的while循环,包括基本语法和多个实例演示,如计算阶乘、求和、猴子吃桃问题以及弹性小球下落问题。通过这些例子,读者可以深入理解while循环在解决实际问题中的应用。
/*-----循环 -----
  可控次数的重复执行

* */
/*while(判定条件){
循环体  条件成立 执行的代码

*/
// int n = 7;//条件 总次数
// int count = 1;//当前是第几行
//
// while (count<=n) {
// System.out.println(count);
// count++;
// }



//练习:  x!= 1*2*3*4*5*...*x
//       x! = x * (x-1)!
/* fac *=1
* fac *=2    fac *= 当前次数
* fac *=3
* fac *=4
* ....
* fac *=x  
*/
// int x = 17;
// int fac = 1; //记录最终结果
// int count = 1;//当前次数
//
// while (count<=x) {
// fac *= count;
// count++;
// }
//
// System.out.println(x+"!=" +fac);

//练习:  sum= 1+3+5+7+9+...+n
// int n = 5;
// int sum = 0; //记录最终结果
// int count = 1;//当前次数
//
// while (count<=(n+1)/2) {
// sum += 2*count-1;  //sum += 新数
// count++;
// }
//
// System.out.println(sum);

//练习: 猴子吃桃   每天吃 现有的 1/2 又多一个
//    10天后 还剩 1 问第一天有几个桃子
// 1 
// (1+1)*2
// ((1+1)*2+1)*2
// .....

// int n1 = 2; //几分之几
// int n2 = 1; //多几个
// int n3 = 10;//总天数
// int n4 = 1; //剩余
//
// int count = 1;//当前次数
// int starNum = 1;
//
// while (count<n3) {
// System.out.println(starNum);
// starNum = (starNum+n2)*n1; 
// count++;
// }
//
// System.out.println(starNum);


//练习:弹性小球  下落上弹    初始高度  10  
//      每次上弹 3/4   最小高度小于  0.001 认为静止
//      求总路程
double maxH = 10; //
double pre = 0.75;
double minH = 0.001;

double s = 0; //记录结果
double h = maxH;//每次开始下落的高度

while (h>=minH) {
//s 增加
s += (1+pre)*h;
h *= pre;
}

System.out.println(s);

}


}
/* * Copyright 2019 TP-Link Technologies Co., Ltd. All rights reserved. * * */ #include "nano_loop.h" struct nano_loop_context* nano_loop_new(void) { struct nano_loop_context *ctx = osal_malloc(sizeof(struct nano_loop_context)); if (ctx) { memset(ctx, 0, sizeof(struct nano_loop_context)); INIT_LIST_HEAD(&(ctx->timeouts)); } return ctx; } static void nano_loop_run_events(struct nano_loop_context *ctx, int timeout) { struct nano_loop_fd *sock; nano_switch_fd_t fds[NANO_LOOP_MAX_REGISTERED_FDS], fd; SH_INT32 i, ret, len; if (ctx->pending_fds_num <= 0) { for (i = 0; i < ctx->registered_num; i++) fds[i] = ctx->registered_fds[i]->fd; len = ctx->registered_num; ret = nano_switch_select(fds, &len, timeout); if (ret == 0 && len > 0) { ctx->pending_fds_num = len; for (i = 0; i < len; i++) ctx->pending_fds[i] = fds[i]; } } if (ctx->pending_fds_num > 0) { fd = ctx->pending_fds[ctx->pending_fds_num - 1]; ctx->pending_fds_num--; for (i = 0; i < ctx->registered_num; i++) { sock = ctx->registered_fds[i]; if (sock->fd == fd) { sock->cb(sock, NANO_LOOP_READ); /* registered_fds array may be changed in cb, since we need process cb only once, so we must break here */ break; } } } } int nano_loop_fd_add(struct nano_loop_context *ctx, struct nano_loop_fd *sock) { if (sock->registered) nano_loop_fd_delete(sock); if (!ctx) ctx = sock->ctx; else sock->ctx = ctx; if (!ctx || ctx->registered_num >= NANO_LOOP_MAX_REGISTERED_FDS || sock->fd == NULL) return -1; ctx->registered_fds[ctx->registered_num++] = sock; sock->registered = TRUE; return 0; } int nano_loop_fd_delete(struct nano_loop_fd *fd) { int i; if (!fd->registered || !fd->ctx) return 0; fd->registered = FALSE; for (i = 0; i < fd->ctx->registered_num; i++) { if (fd->ctx->registered_fds[i] == fd) { fd->ctx->registered_fds[i] = fd->ctx->registered_fds[fd->ctx->registered_num - 1]; fd->ctx->registered_num--; break; } } return 0; } int nano_loop_timeout_add(struct nano_loop_context *ctx, struct nano_loop_timeout *timeout) { struct nano_loop_timeout *tmp; struct list_head *h; if (timeout->pending) return -1; if (!ctx) ctx = timeout->ctx; else timeout->ctx = ctx; if (!ctx) return -1; h = &(ctx->timeouts); list_for_each_entry(tmp, &(ctx->timeouts), list) { if (tmp->time > timeout->time) { h = &tmp->list; break; } } list_add_tail(&timeout->list, h); timeout->pending = TRUE; return 0; } int nano_loop_timeout_set(struct nano_loop_context *ctx, struct nano_loop_timeout *timeout, int msecs) { if (msecs < 0) return -1; if (timeout->pending) nano_loop_timeout_cancel(timeout); timeout->time = osal_time_ms() + msecs; return nano_loop_timeout_add(ctx, timeout); } int nano_loop_timeout_cancel(struct nano_loop_timeout *timeout) { if (!timeout->pending) return -1; list_del(&timeout->list); timeout->pending = FALSE; return 0; } int nano_loop_timeout_remaining(struct nano_loop_timeout *timeout) { SH_UINT64 tv; if (!timeout->pending) return -1; tv = osal_time_ms(); return timeout->time - tv; } static int nano_loop_get_next_timeout(struct nano_loop_context *ctx, SH_UINT64 tv) { struct nano_loop_timeout *timeout; if (list_empty(&(ctx->timeouts))) return -1; timeout = list_first_entry(&(ctx->timeouts), struct nano_loop_timeout, list); if (timeout->time <= tv) return 0; else return timeout->time - tv; } static void nano_loop_process_timeouts(struct nano_loop_context *ctx, SH_UINT64 tv) { struct nano_loop_timeout *t; while (!list_empty(&(ctx->timeouts))) { t = list_first_entry(&(ctx->timeouts), struct nano_loop_timeout, list); if (t->time > tv) break; nano_loop_timeout_cancel(t); if (t->cb) t->cb(t); } } static void nano_loop_clear_timeouts(struct nano_loop_context *ctx) { struct nano_loop_timeout *t, *tmp; list_for_each_entry_safe(t, tmp, &(ctx->timeouts), list) nano_loop_timeout_cancel(t); } int nano_loop_run(struct nano_loop_context *ctx) { SH_UINT64 tv; ctx->nano_loop_cancelled = FALSE; while (!ctx->nano_loop_cancelled) { tv = osal_time_ms(); nano_loop_process_timeouts(ctx, tv); if (ctx->nano_loop_cancelled) break; tv = osal_time_ms(); nano_loop_run_events(ctx, nano_loop_get_next_timeout(ctx, tv)); } return 0; } void nano_loop_done(struct nano_loop_context *ctx) { if (ctx) { nano_loop_clear_timeouts(ctx); osal_free(ctx); } } 分析一下这些代码的用途和原理
最新发布
12-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值