黑马程序员技术交流社区

标题: 谁会做这题?求破解。。。 [打印本页]

作者: bianzhiguo6    时间: 2015-3-20 15:25
标题: 谁会做这题?求破解。。。
           分别使用   “foreach语言” 和  “while循环语句 ”    遍历该数组
        数组:  {赤,橙,红,绿,青,蓝,紫}

作者: 女神从来不加班    时间: 2015-3-20 16:20
  1. /*
  2. * 是这个意思吗?
  3. */
  4. public class Question {

  5.         public static void main(String[] args) {

  6.                 String[] strArr = { "赤", "橙", "红", "绿", "青", "蓝", "紫" };
  7.                 System.out.println("while方法");
  8.                 whileMethod(strArr);
  9.                 System.out.println("for方法");
  10.                 foreachMethod(strArr);

  11.         }

  12.         private static void whileMethod(String[] arr) {
  13.                 int counter = 0;
  14.                 while (counter < arr.length) {
  15.                         System.out.print(arr[counter++] + " ");
  16.                 }
  17.                 System.out.println();
  18.         }

  19.         private static void foreachMethod(String[] arr) {
  20.                 for (String s : arr) {
  21.                         System.out.print(s + " ");
  22.                 }
  23.                 System.out.println();
  24.         }
  25. }
复制代码

作者: 李章敏    时间: 2015-3-20 17:14
楼上正解
作者: 张金龙    时间: 2015-3-20 18:11
原来foreach语言就是高级FOR循环。
作者: bianzhiguo6    时间: 2015-3-21 10:14
女神从来不加班 发表于 2015-3-20 16:20

很给力,佩服。
作者: bianzhiguo6    时间: 2015-3-21 10:15
李章敏 发表于 2015-3-20 17:14
楼上正解

哈哈,楼上的相当可以
作者: bianzhiguo6    时间: 2015-3-21 10:18
张金龙 发表于 2015-3-20 18:11
原来foreach语言就是高级FOR循环。

foreach语句是for语句的特殊简化版本,不能完全取代for语句
作者: tubao1991    时间: 2015-3-21 10:55
来看看会不会!
作者: 唐泉银    时间: 2015-3-21 19:06
膜拜楼上
作者: 温大帅    时间: 2015-3-21 22:53
学习学习
作者: bianzhiguo6    时间: 2015-3-22 23:57
温大帅 发表于 2015-3-21 22:53
学习学习

恩,对的,要好好学了
作者: bianzhiguo6    时间: 2015-3-22 23:58
唐泉银 发表于 2015-3-21 19:06
膜拜楼上

向大神看齐
作者: bianzhiguo6    时间: 2015-3-22 23:59
tubao1991 发表于 2015-3-21 10:55
来看看会不会!

难吗?

作者: Peng苏    时间: 2015-3-23 00:17
我不会啊!!!!!!
作者: 932773877    时间: 2015-3-23 00:25
我还在前往62期学员的路上,只知道while,希望能帮到你。
分别使用   “foreach语言” 和  “while循环语句 ”    遍历该数组
        数组:  {赤,橙,红,绿,青,蓝,紫}
public class Yanse{
         public   static   void   main(String[]  args){
           Yanse();

}
Yanse(){
        String  yanse[]=new  yangse[]{赤,橙,红,绿,青,蓝,紫};
        int a=0;
        swile(a<yangse.lenght){
         System.out.print(yanse[a]);

}
}
}
作者: wkz_crystal    时间: 2015-3-23 00:57
【重点】foreach:说简单点就是一个子元素在数组或者是集合中的遍历……
所以“女神从来不加班”的代码非常ok的

作者: 胡谭龙    时间: 2015-3-23 21:24
正解 写的不错
作者: bianzhiguo6    时间: 2015-3-24 09:44
wkz_crystal 发表于 2015-3-23 00:57
【重点】foreach:说简单点就是一个子元素在数组或者是集合中的遍历……
所以“女神从来不加班”的代码非常 ...

正解,对的
作者: bianzhiguo6    时间: 2015-3-24 09:45
胡谭龙 发表于 2015-3-23 21:24
正解 写的不错

向他学习啊
作者: daveyang    时间: 2015-3-24 10:03
学习了,高级for循环,一楼的书写很规范




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2