方法的返回值类型 和实际反悔的类型 自动类型转换
交互方式
图形化界面
命令行方式:dos(不方便)
vim a.java//新建
javac a.java//编译 生成a.class
java a//运行
class demo
{
//主函数(必须要有)
public static void main(string[] args)
{
system.out.printin("hello java");
}
}
如果任意目录下执行程序
/java/a
类中的命名区间
class demo
{
//类的区间
}
类型转换
class demo
{
//主函数(必须要有)
public static void main(string[] args)
{
system.out.printin(“hello java”);
byte b=3;
b=b+2;
system.out.println(b);//错误
}
}
byte b=3;//判断3是否在b的范围 3是可以 判断3在b的范围内
b=b+2;//b是一个变化的值 精度丢失
(提升)+***
+
先把两个数据提升为一种类型才能计算
类型强制转换
b=(byte)(b+2);
b=()b+2//不可以 只将b强制转换
用处 结果是小数 保留整数 的时候
bin java可运行程序
jre java运行
java特征: 跨平台 编译完成可以在任何操作系统运行
//如何跨平台?class()成为字节码 java自定义的语言格式 这种格式和任何操作系统没有关系
//运行:目标程序是class文件 前提是java自定义
class->java 运行器解释成当前操作系统识别的指令
分布式
public 当前类的访问级别 公共类 文件名必须和公共类类名一致 所以只能有一个共有类
class是一个关键字 表示当前的类
demo 是类名 一个单词 也可以是多个单词 每个单词的首字母大写
物理文件定义一个类(一个屋里文件可以存在多个类
//中文编码集 相当于密钥 文件存储 存储在磁盘上(二进制形式)
//数据类型:基本数据类型 引用数据类型(指针)
//基本:1。整型 (byte short短整形2字节 int整型4字节 long长整型8字节)
//2.浮点型(float4字节 double8字节)
//3.boolean类型 true false
//4.字符型 char两字节 unicode包含中文 c的char占一字节
//运算符
//1.算数运算符(±)
//2.关系
//3.位
//& && 区别 &作为位运算 非短路式 &&只能是逻辑运算符
//4.逻辑
//5.赋值
//6.三目运算符
public class demo
{
//main函数 这个方法是公开级别
public static void main(String[] args)
{
int i=20;
int j=2;
System.out.println("gg");
//System->结构体 java类来源于结构体 out也是一个结构体 print()是函数
}
}
练习:控制台打出日历
java包(package)
包的作用
- 把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用。
- 如同文件夹一样,包也采用了树形目录的存储方式。同一个包中的类名字是不同的,不同的包中的类的名字是可以相同的,当同时调用两个不同包中相同类名的类时,应该加上包名加以区别。因此,包可以避免名字冲突。
- 包也限定了访问权限,拥有包访问权限的类才能访问某个包中的类。
java Scanner类
我们可以通过 Scanner 类来获取用户的输入。
下面是创建 Scanner 对象的基本语法:
Scanner s = new Scanner(System.in);
导入java、基础类
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// next方式接收字符串
System.out.println("next方式接收:");
// 判断是否还有输入
if (scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
结果
$ javac ScannerDemo.java
$ java ScannerDemo
next方式接收:
runoob com
输入的数据为:runoob
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判断是否还有输入
if (scan.hasNextLine()) {
String str2 = scan.nextLine();
System.out.println("输入的数据为:" + str2);
}
scan.close();
}
}
$ javac ScannerDemo.java
$ java ScannerDemo
nextLine方式接收:
runoob com
输入的数据为:runoob com
next() 与 nextLine() 区别
- next():
- 一定要读取到有效字符后才可以结束输入。
- 对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
- next() 不能得到带有空格的字符串。
- nextLine():
- 以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
- 可以获得空白。
有了这两个基础 练习一下日历的打印
/*
* 时间:2018-3-25
* 目的:practice
* 内容:利用Java输出电子日历
* 使用Java技术实现电子日历,需求为从控制台输入年份与月份后,控制台输出该月的日历信息。
* */
import java.util.Scanner;
public class Calendar {
public static void main(String[] a) {
// 声明赋值
int year;// 年份
int month;// 月份
Scanner read = new Scanner(System.in);// 扫描器
int pinday[] = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };// 平年天数分布
int runday[] = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };// 闰年天数分布
int sumdays = 0;// 总天数
int week;
// 输入提示
System.out
.print("***************************欢迎使用万年历***********************************\n");
System.out.print("请输入年份:");
year = read.nextInt();
System.out.print("\n请输入月份:");
month = read.nextInt();
// 天数计算
int day = (year - 1900) * 365 + (year - 1900) / 4;// 相距年天数
if (year % 4 == 0&&year % 400 == 0&&year % 100 != 0) {// 闰年天数
for (int i = 0; i < month; i++) {
sumdays = sumdays + runday[i]; // 月份天数求和
}
sumdays = sumdays + day - runday[month - 1];// 距目的月总天数(目的月天数除外)
} else {// 平年天数
for (int i = 0; i < month; i++) {
sumdays = sumdays + pinday[i]; // 月份天数求和
}
sumdays = sumdays + day - pinday[month - 1];// //距目的月总天数(目的月天数除外)1900-1901相差366天
}
// System.out.print(sumdays+"\n");
// 星期计算
week = (sumdays) % 7;// 求月的第一天星期数
System.out
.print("---------------------------------------"+month+"月--------------------------------------\n");
System.out.print("\t星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
switch (week) {// 制表符对齐
case 1:
System.out.print("\t\t");
break;// 目的月第一天星期一
case 2:
System.out.print(" \t\t\t\t");
break;// 目的月第一天星期二
case 3:
System.out.print(" \t\t\t\t\t\t");
break;// 目的月第一天星期三
case 4:
System.out.print(" \t\t\t\t\t\t\t\t");
break;// 目的月第一天星期四
case 5:
System.out.print(" \t\t\t\t\t\t\t\t\t\t");
break;// 目的月第一天星期五
case 6:
System.out.print(" \t\t\t\t\t\t\t\t\t\t\t\t");
break;// 目的月第一天星期六
case 0:
System.out.print("");
break;// 目的月第一天星期天
}
if (year % 4 == 0) {// 闰年月打印
for (int i = 1, w = week; i <= runday[month - 1]; i++, w++) {
if (w % 7 == 0 && i != 1) {// 累积星期到星期日且不是月的第一天
System.out.println(""); // 换行
System.out.print(" "); // 新起行空格对齐
w = 0;// 星期累积达周日重新起一周
}
System.out.print("\t " + i + " \t"); // 打印天数
}
} else {
for (int i = 1, w = week; i <= pinday[month - 1]; i++, w++) {
if (w % 7 == 0 && i != 1) {// //累积星期到星期日且不是月的第一天
System.out.println(""); // 换行
System.out.print(" "); // 新起行空格对齐
w = 0;// 星期累积达周日重新起一周
}
System.out.print("\t " + i + " \t"); // 打印天数
}
}System.out.println();
System.out
.print("----------------------------------------------------------------------------------");
}
}
java输出保留小数
System.out.println(String.format("%.2f", f));
f为要输出的结果