1、基本输出语句

/*
* java
* 多行注释
*/
//java单行注释
public class _01_HelloWorld {
public static void main(String[] args) {// main方法
System.out.print("Hello Java!");// 使用print,最后无换行
System.out.println("你好 Java!");// 使用println,最后有换行
System.out.println(123 + 321);// 使用数字
System.out.println('S');// 使用字符
System.out.println("123\t456\tC:\\myfiles\\123.txt");// 使用转义字符
System.out.println("十进制的10是"+10+"。");// 拼接字符串和数字
System.out.println("八进制的10是"+010+"。");// 拼接字符串和数字
System.out.println("16进制的10是"+0x10+"。");// 拼接字符串和数字
System.out.println("16进制的F是"+0xF+"。");// 拼接字符串和数字
}
}

2、各种类型的变量

package Java_basic;

public class _02_DataType {
public static void main(String[] args) {
//大范围数不能赋值给小范围的,如double不能赋值给int
boolean boo = true;
char ch = 'a'; // 布尔型
byte by = 32; // 字节型,-128~127
short sh = 66; // 短整型,2个字节,-32768~32767
int i = 44; // 整型,4个字节,-2147483648~2147483647
long lo = 9; // 长整型,8个字节,-9223372036854775808~9223372036854775807
float flo = 4; // 4字节
double dou = 5.6; // 8字节
System.out.println(boo);
System.out.println(ch);
System.out.println(by);
System.out.println(sh);
System.out.println(i);
System.out.println(lo);
System.out.println(flo);
System.out.println(dou);
}
}

3、从键盘读取字符并输出

3.1 程序

import java.io.*;//库

public class _03_KeybordInput {
public static void main(String[] args) {
// 流对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 调用流对象的方法,将读取的值赋给str
String str = "INI";
try {
str = br.readLine();
} catch (IOException e) {
// ignore,忽略异常
}
// 输出str
System.out.println(str);
}
}

3.2 报错&解决

// 流对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 调用流对象的方法,将读取的值赋给str
String str = br.readLine();

报错:

Unhandled exception type IOException

原因:

没有异常处理

解决:

String str;
try {
str = br.readLine();
} catch (IOException e) {
// ignore,忽略异常
}

4、从键盘读取数值

将刚才读取的字符串str转换为int型变量num

import java.io.*;//库

public class _03_KeybordInput {
public static void main(String[] args) {
// 流对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 调用流对象的方法,将读取的值赋给str
String str = "INI";
try {
str = br.readLine();
} catch (IOException e) {
// ignore,忽略异常
}
//将str转换为数字
int num=Integer.parseInt(str);
// 输出一些结果
System.out.println("str="+str);
System.out.println("num="+num); }
}

5、读入多个数值

//输入多个数值,计算
//必须以回车分割输入的数字,否则异常
String str1 = "str1";
String str2 = "str2";
try {
str1 = br.readLine();
} catch (IOException e) {
// ignore,忽略异常
}
try {
str2 = br.readLine();
} catch (IOException e) {
// ignore,忽略异常
}
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
int num3 = num1 + num2;
// 输出一些结果
System.out.println("num1=" + num1);
System.out.println("num2=" + num2);
System.out.println("num1+num2=" + num3);

6、运算符及计算

/*
* 操作符:operator
* 操作对象:operand
* 四则运算:左结合
* 赋值运算:右结合
*/
public class _04_Expression {
public static void main(String[] args) {
System.out.println("5+6=" + (5 + 6));
System.out.println("5/6=" + (5 / 6) + "整数间除法默认向下取整");
int a = 0;
System.out.println("a++=" + a++);
System.out.println("a++=" + a++);
System.out.println("a++=" + a++);
System.out.println("a++=" + (a * 2 + 5));
System.out.println("a * (2 + 5)=" + (a * (2 + 5)));
System.out.println("a=" + a);
System.out.println("(a = a * 2 + 5) =" + (a = a * 2 + 5));
System.out.println("a=" + a);
// 数据类型转换
int intnum = 5;
double dounum = intnum;// 小的数据类型可以赋值给大的类型
System.out.println("intnum=" + intnum);
System.out.println("dounum=" + dounum);
double dounum1 = 5.5;// 大的数据类型不可以赋值给小的类型
// int intnum1=dounum1;// 报错:Type mismatch: cannot convert from double to int
// 不同数据类型间的计算:会把小的转成大的
int d = 2;
double pi = 3.14;
System.out.println("直径是" + d + "cm的圆。");
System.out.println("周长是" + (pi * d) + "cm。");
System.out.println("面积是" + (pi * d * d / 4) + "cm。");
// 强制类型转换符
double dounum2 = 9.9;
System.out.println("dounum2 = " + dounum2);
System.out.println("(int)dounum2 = " + (int) dounum2); } }

参考:Easy Java (第7版) (日)高桥麻奈

最新文章

  1. 【转】The import javax.servlet cannot be resolved
  2. CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\.。。”--“拒绝访问
  3. checked属性
  4. $(window).load(function() {})和$(document).ready(function(){})的区别
  5. 前端入门级之如何从零开始前端(估计要被人鄙视成LOW货了)入门篇
  6. 解决eclipse+git中每次clean项目需要重新commit文件
  7. SDL2.0的SDL_Event事件处理
  8. NOIP第7场模拟赛题解
  9. Mindjet MindManager 2016/2017 折腾记录
  10. 多IDC数据分布--MySQL多机房部署 - 学习笔记 - 51CTO技术博客
  11. iOS开发中frame与bounds的区别
  12. NLP —— 图模型(二)条件随机场(Conditional random field,CRF)
  13. 解决iar试调时程序无法进入主函数的问题
  14. 2017-2018-1 20155201 《信息安全系统设计基础》 pwd命令的实现
  15. MySQL学习笔记_6_SQL语言的设计与编写(下)
  16. Winsock编程基继承基础(网络对时程序)
  17. shell编程基础(一): 基本变量和基本符号
  18. March 06th, 2018 Week 10th Tuesday
  19. ORA-4031 错误故障排除与诊断[视频] (Doc ID 2016002.1)
  20. Unity5 图形系统介绍 学习

热门文章

  1. 关键字break和continue
  2. CPU 和 CPU Core 有啥区别?多核 CPU?多个 CPU?
  3. avue属性详解和使用介绍
  4. uniapp详细入门教程
  5. STL vector常用API
  6. MyBatis四大参数两种写法
  7. 默认方法:negate-集合信息筛选
  8. java入门与进阶P-1.7+P-1.8
  9. QtQuick使用MediaPlayer抓取摄像头影响报错Error: "Your GStreamer installation is missing a plug-in."
  10. 【分析笔记】Linux gpio_wdt.c 看门狗设备驱动源码分析