zoukankan      html  css  js  c++  java
  • java的几个入门小程序

    1.先是最最经典的hello world!

    public class Hello {
        public static void main(String args[])
     {
             System.out.println("hello world!");
         }
    }
    hello world!

    2.print与println的区别

    public class Hello {
        public static void main(String args[])
     {
            System.out.print("hello world!"); 
            System.out.println("hello world!");
            System.out.println("hello world!");
         }
    }
    hello world!hello world!
    hello world!

    显然println结束后有换行而print没有,所以用println比较好

    3.println的用法

    public class Hello {
        public static void main(String args[])
     {
            int x=2;
            System.out.println(x+"*"+x+"="+(x*x));
         }
    }
    2*2=4

    这里要输出的数字用“+”号连接

    4.常量的声明

    public class Hello {
            static final int YEAR=365;
            public static void main(String args[]) {
            System.out.println("两年等于"+2*YEAR+"天");
         }
    }
    两年等于730天
    public class Hello {
            public static void main(String args[]) {
            final int YEAR=365;
            System.out.println("两年等于"+2*YEAR+"天");
         }
    }
    两年等于730天

    常量赋值语法final+数据类型+变量名称=变量值,当常量用于一个类的成员变量时,必须给常量赋值。

    5.变量的声明

    public class Hello {
            public static void main(String args[]) {
            int num=3;
            char ch='a';
            System.out.println(num+"是整数");
            System.out.println(ch+"是字符");
         }
    }
    3是整数
    a是字符

       这些程序均是从java书中摘抄的,将书中的代码自己用编译器run一遍,对于像我这种初学者很有好处。

  • 相关阅读:
    转Vtype扩展
    Can't connect to MySQL server on 'ip' (13)
    观察者+js 模式
    (转)ASP.NET架构分析
    sql得到时间
    Js+XML 操作 (转)
    js中的math对象
    property和attribute的区别
    CSS样式定义
    linux 开启 mount
  • 原文地址:https://www.cnblogs.com/jahnson/p/8570747.html
Copyright © 2011-2022 走看看