美文网首页
Java基本语法(2)

Java基本语法(2)

作者: OldSix1987 | 来源:发表于2016-09-05 02:50 被阅读27次
    public class HelloWorld{
    public static void main(String[] args){
            int num = 999;
            int count = 0;
            
            // 这里需要注意,必须要标明int i,否则会报错,java的语法很严格
            for(int i=1;i<10;i,++) {
                if (num < 10) {,
                    count += 1; 
                    break;
                }
                num /= 10;
                count++;
            }
            // 格式化输出记得用printf,其他的跟c差不多
            System.out.printf("它是个%d位的数!", count);
    
        }
    }
    

    new一个数组时:
    (1)需要指定数组的长度;
    (2)在声明数组的同时赋值时不能指定数组长度
    (3)数组长度:array.length

    int[] scores = new int[5];
    double[] height = new double[5];
    Stringp[] names = new String[5];
    
    int[] scores = {78,91,84,68};
    int[] scores = new int[]{78,91,84,68};
    

    java.util.Arrays
    java.util.Scanner


    二维数组:

    int[][] num = new int[2][3];
    int[][] num = {{1,2,3},{4,5,6}};
    
    int[][] num = new int[3][]; // 定义一个三行的二维数组
    num[0] = new int[2]; // 为第一行分配两列
    num[1] = new int[3]; // 为第二行分配三列
    num[2] = new int[4]; // 为第三行分配四列
    

    Function`

    public int calcAvg(int[] nums) {
        return sum / nums.length;
    }
    

    相关文章

      网友评论

          本文标题:Java基本语法(2)

          本文链接:https://www.haomeiwen.com/subject/vzcrettx.html