845
技术社区[云栖]
JAVA编程思想第四版—第三章—习题与答案
(1) 写一个程序,打印出1 到100 间的整数。
public class test{ public static void main(String[] args){ int i; for(i=0;i<=100;i++){ //if(i==47) break; //if(i==47) return; System.out.println(i+" "); } } }
(2) 修改练习(1),在值为47时用一个break退出程序。亦可换成return试试。
(3) 创建一个switch 语句,为每一种case 都显示一条消息。并将 switch置入一个for循环里,令其尝试每一种case。在每个case 后面都放置一个break,并对其进行测试。然后,删除break,看看会有什么情况出现。
import java.util.*; public class test{ public static void main(String[] args){ int month; Date now=new Date(); month=now.getMonth()+1; switch(month) { case 1:System.out.println("Januay");break; case 2:System.out.println("February");break; case 3:System.out.println("March");break; case 4:System.out.println("April");break; case 5:System.out.println("May");break; case 6:System.out.println("June");break; case 7:System.out.println("July");break; case 8:System.out.println("August");break; case 9:System.out.println("September");break; case 10:System.out.println("October");break; case 11:System.out.println("November");break; case 12:System.out.println("December");break; default: break; } } }
输出:
November
修改后:
import java.util.*; public class test{ public static void main(String[] args){ int month; Date now=new Date(); month=now.getMonth()+1; switch(month) { case 1:System.out.println("Januay");break; case 2:System.out.println("February");break; case 3:System.out.println("March");break; case 4:System.out.println("April");break; case 5:System.out.println("May");break; case 6:System.out.println("June");break; case 7:System.out.println("July"); case 8:System.out.println("August"); case 9:System.out.println("September"); case 10:System.out.println("October");break; case 11:System.out.println("November"); case 12:System.out.println("December");break; default: break; } } }
输出:
November
December
最后更新:2017-04-02 06:52:03
上一篇:
MYSQL 存储过程和函数 案例 例子
下一篇:
Magento以手机号和密码登录简介
比传统检测污染物分子精准的解决方案:超薄纳米传感器光学指纹检测
当当网开放战略能否“稳当当”
【usaco】4.4.2最小割集PROB Pollutant Control
QQ v4.7.0全新UI
WebLogic中修改端口号和省略端口号的做法
Ribbon2 核心设计分析
阿里云全球首批MVP杨绣专访 - 技术要坚持刨根问底
JSP网页分页显示数据库的内容
码栈开发手册(二)---编码方式开发(中级级课程④)
Interview with iDST Deputy Managing Director Hua Xiansheng: City Brain – Comprehensive Urban Cognition