【每日代码】(7/21)

一段简单的输出数组的代码。复习了数组的知识。
如下——

package cn.jsu.www6;

import java.util.Scanner;

import java.util.Arrays; /* 数组工具类 */

public class Brave {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] j = new int[5];
        j[0] = 1;
        for (int l = 1; l < j.length; l++) {/* 数组名.length求长度 */
            j[l] = j[l - 1] + 2;
        }
        String k = Arrays.toString(j);/* toString 将一维数组转换成字符串 */
        System.out.println("【数组1】:\t" + k);

        String[][] two = { /* 不规则二维数组 */
                {"a", "b", "c"},
                {"1", "2"},
        };
        System.out.print("【数组2】:\t");
        for (String[] one : two) {/* foreach 遍历不规则二维数组 two */
            for (String x : one) {
                System.out.print(x + " ");
            }
        }
        System.out.println();
        System.out.println("\n输入1输出【数组1】普通形式————");
        byte key = scan.nextByte();
         if((key == 1 ? 1 : 0) == 1){/* 条件表达式 */
             fun(1, 2, j);
         }
    }
    public static void fun(int a, int b, int... x) {
        int i = 0;
        for(int z:x){
            if(i % 2 == 0) { /* 取余判断奇偶 */
                z += a;
                i ++;
            } else {
                z += b;
                i ++;
            }
        }
        System.out.println();
        System.out.print("【数组3】:\t");
        for(int j = 0;j < 5; j++) {
            System.out.print(x[j] + " ");
        }
    }
}

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值