A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© a2300538 中级黑马   /  2015-11-26 21:12  /  1017 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

不用循环和if语句输出1-200..   有想到过递归..  但貌似也要用到if啊..

10 个回复

正序浏览
回复 使用道具 举报
  1. class itheima{
  2.        
  3.         public static void main(String[] args){
  4.                 print(1);
  5.         }
  6.         public static void print(int x){
  7.                 try{
  8.                 System.out.print(x+"\t");
  9.                 int a = x;
  10.                 a = 1/(a-200);//当输出到200时,会抛出异常,程序停止
  11.                 print(++x);
  12.                 }catch(Exception e){
  13.                         System.out.println("输出完毕");
  14.                 }
  15.                
  16.         }
  17. }
复制代码
回复 使用道具 举报
回复 使用道具 举报
wqd123 中级黑马 2015-11-27 22:01:40
7#
6666666666666666
回复 使用道具 举报
没有循环,没有if  坐等大神来解
回复 使用道具 举报
  1. public class Demo_Print200 {
  2.         public static void main(String[] args) {               
  3.         int i = 1;       
  4.         print(i);
  5.                
  6.         }
  7.        
  8.         private static void print(int i) {                               
  9.                 try {
  10.                         System.out.println(i);
  11.                         print(i >= 200 ? 1/0 : ++i);
  12.                 } catch (Exception e) {               
  13.                         System.out.println("输出完毕");               
  14.                 }                       
  15.         }
  16. }
复制代码

点评

main方法 里面不用定义i 直接在方法里传入 1 把try里面的 >= 改成 == 就行了 没必要 >=  发表于 2015-12-1 23:50
回复 使用道具 举报 1 0
这个我觉得用for循环更好吧!!
回复 使用道具 举报
一个文本里面写1~200
然后 IO流读取输出算不算
把缓冲调大点应该就不用循环...

不知算不算建议...
回复 使用道具 举报
呵呵 前面看到一个  http://bbs.itheima.com/thread-263550-1-1.html
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马