Java02 Java程序结构

程序的主结构:

 一个完整的Java程序,它的基本结构包括:class类、main方法、输出语句、注释。

命名规范和代码书写

新建项目命名

项目命名时可用数字,字母,下划线,$来命名。(注意:命名时数字不可作为开头,也不能是关键字,另外命名时采用大驼峰命名,即开头首字母大写,后面的小写,如User Date)。

代码书写

 public class  类名(与新建项目名保持一致);{
 public static void main(string[ ] args);(定义一个main方法,即程序的主入口){
         程序操作内容
    }
  } 

输出语句的表达

输出语句主要有以下三种输出方式:

 换行输出:system.out.println()
 不换行输出:system.out.print()
 格式化输出:system.out.prinf()

注意:对于汉字的输出,由于计算机无法编译,编译时需要加上javac-encoding urf-8的命令。

程序输出可有以下内容

输出数字

输出计算结果。

输出字符串。

输出字符串与数字的混合表达式。

输出 math类常用方法

System.out.println("圆周率:" + Math.PI); 3.141592653589793
 ​
 System.out.println("常数e"+Math.E); //2.71828
 ​
 System.out.println("取整数:" + Math.round(6.71)); 对6.71进行四舍五入,结果为7
 ​
 System.out.println("绝对值:"+Math.abs(-7.8));// 7.8
 ​
 System.out.println("最大值:"+Math.max(100, 99));//100
 ​
 System.out.println("最小值:"+Math.min(1.2, 2.3));//1.2
 ​
 System.out.println("随机数:" + Math.random()); // 0-1之间的随机数
 ​
 System.out.println("随机数(10以内小数):" + Math.random() \* 10);
 ​
 System.out.println("随机数(10以内整数):" + Math.round(Math.random() \* 10));
 ​
 System.out.println("幂运算:" + Math.pow(3, 9)); // 3的9次幂
 ​
 System.out.println("平方根:" + Math.sqrt(16)); // 16的平方根
 ​
 System.out.println("计算e的x次方"+Math.exp(2));// 7.389...
 ​
 System.out.println("计算以e为底的对数:"+Math.log(4));
 ​
 System.out.println("计算以10为底的对数:"+Math.log10(100));

输出当前日期时间

 System.out.println(LocalDate.now()); // 当前计算机的日期
 ​
 System.out.println(LocalTime.now()); // 当前计算机的时间
 ​
 System.out.println(LocalDateTime.now()); // 当前计算机的日期时间
 ​
 System.out.println("年份:" + LocalDate.now().getYear());
 ​
 System.out.println("月份:" + LocalDate.now().getMonth());
 ​
 System.out.println("月份:" + LocalDate.now().getMonthValue());
 ​
 System.out.println("日期:" + LocalDate.now().getDayOfMonth());
 ​
 System.out.println("星期:" + LocalDate.now().getDayOfWeek());

输出当前日期时间:

 System.out.println(LocalDate.now()); // 当前计算机的日期
 System.out.println(LocalTime.now()); // 当前计算机的时间
 System.out.println(LocalDateTime.now()); // 当前计算机的日期时间
 System.out.println("年份:" + LocalDate.now().getYear());
 System.out.println("月份:" + LocalDate.now().getMonth());
 System.out.println("月份:" + LocalDate.now().getMonthValue());
 System.out.println("日期:" + LocalDate.now().getDayOfMonth());
 System.out.println("星期:" + LocalDate.now().getDayOfWeek());

转义符

 \t制表符
 \n换行
 \u 16进制编码的ASCII码
 在转义符前面加上“\”,可输出“\n”“\t”“\u”等字符串。

代码注释

代码注释主要是写给程序员看的,因为在代码编译的class文件中并没有注释的存在。注释主要有:

 单行注释//
 ​
 多行注释/* */
 ​
 文档注释
 /*
 * * 
 */
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值