一.数据类型的转换
public class Demo01{
public static void main(String[] args){
//数据类型的转换:
//1.目标类型要与源类型兼容或
//2.目标类型大于源类型
double a = 10;//数据类型的隐式转换
//强制类型的转换
int b = (int)3.14;//注意:强制类型转换会出现丢失精度问题
System.out.println(b);
//输出结果为3
}
}
二.字符串的拼接
public class Demo01{
public static void main(String[] args){
String str1 = "hello";
String str2 = "world";
System.out.println(str1 + str2);
//输出结果为:helloworld;
//字符串在前时,与字符串相加得到的还是字符串
int num1 = 10;
int num2 = 15;
System.out.println("hello" + num1 + num2);
//输出:hello1015
System.out.println(num1 + num2 + "hello");
//输出:25hello
//判断两个字符串是否相等(使用equals返回布尔值)
System.out.println(str1.equals(str2));
//结果为:false;
}
}
//使用分装函数来进行进制之间的转换
public class Demo01{
public static void main(String[] args){
将十进制转化为二进制
String s1 = Integer.toBinaryString(15);
System.out.println(s1);
输出:1111
//十进制转化为八进制
String s2 = Integer.toOctalString(15);
System.out.println(s2);
输出:17
//十进制转化为十六进制
String s3 = Integer.toHexString(15);
System.out.println(s3);
输出:f
}
}
三.运算符
1.算术运算符
包含: + - * / % ++ –
2.关系运算符(主要用来判断条件,使用关系运算符会有返回值,是布尔类型)
包含: > < >= <= == !=
3.逻辑运算符
&& 逻辑与(一假则假)
|| 逻辑或 (一真则真)
! 逻辑非 (非真即假)
public class Demo02{
public static void main(String[] args){
//取余运算
//可用于判断一个数是不是偶数
int num = 10 % 3;
System.out.println("num=" + num);
//输出:1
}
//自增
int n1 = 10;
n1++;//相当于n1 = n1 + 1;
//加加在前先进行自增在进行运算
//加加在后先进行运算再进行自增
int n2 = 10;
int n3 = 10;
int n4 = n2 + n3++;
System.out.println("n3=" + n3);
System.out.println("n4=" + n4);
//输出分别为:11;20
//关系运算符
int n6= 12;
int n7= 15;
boolean b1 = (n6 > n7);
System.out.println(b1);//输出:false
//逻辑运算符
int n8 = 10;
int n9 = 15;
boolean b2 = (n8 > n9) && (n8 > 1);
System.out.println(b2);//输出:false
//短路现象
//逻辑与 当条件1为假时就不会执行条件二了,直接返回结果
boolean b3 = (n8 > n9) && (n8++ > 1);
System.out.println(b3);//输出:false
System.out.println(n8);//输出:10
boolean b4 = (n8 < n9) || (++n9 > 1);
System.out.println(b4);//输出:true
System.out.println(n9);//输出:15
//逻辑非
boolean b5 = true;
boolean b6 = !b5;
System.out.println(b6);//输出:false
}
四.分支结构
4.1 if ;if else; if else if else 语句
public class Demo03 {
public static void main(String[] args) {
// 从控制台输入一个数
// 是偶数但是不能是7的倍数 满足条件输出该数
Scanner num = new Scanner(System.in);
String str1 = num.nextLine();
int number = Integer.parseInt(str1);
if (number % 2==0 && number %7 != 0) {
System.out.println(number);
}
// 输入一个年份 判断是不是 闰年
System.out.println("请输入一个年份");
Scanner num = new Scanner(System.in);
String str2 = num.nextLine();
int year = Integer.parseInt(str2);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year +"是闰年");
}
/* 键盘输入模拟天气
* 如果晴天,去爬山,
* 如果下雨,去逛街,
* 再次接收用户的输入
* 小雨的话,带上伞,
* 暴雨的话,穿上雨衣
*/
System.out.println("请输入天气");
Scanner t1 = new Scanner(System.in);
String str3 = t1.nextLine();
if (str3.equals("晴天")) {
System.out.println("去爬山");
}else if (str3.equals("下雨")) {
System.out.println("去逛街");
System.out.println("请输入大小雨");
//Scanner t2 = new Scanner(System.in);
//scanner对象创建一次就可以,可以接收多个键盘输入的值
String str4 = t1.nextLine();
if (str4.equals("小雨")) {
System.out.println("带雨伞");
}else if (str4.equals("暴雨")) {
System.out.println("穿雨衣");
}
}
}
//成绩大于90分优秀,70至90良好,
// 60至70合格,60及以下不及格
//键盘输入一个数
//创建从键盘接受字符串对象
System.out.println("请输入一个成绩");
Scanner in = new Scanner(System.in);
//in 接收输出的的字符串
String str = in.nextLine();
//将字符串转化成整型
int score = Integer.parseInt(str);
//判断
if(score > 90) {
System.out.println("优秀");
}else if (score >70) {
System.out.println("良好");
}else if(score > 60) {
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
4.2switch语句
switch 后面小括号中表达式的值必须是整型或字符型
如果判断的是一个范围使用if语句
如果判断的是具体的几个值使用switch语句
switch (key->被匹配的值) {
case value->匹配的值1:
//值匹配成功执行的语句
break;//跳出switch语句
case value->匹配的值2:
//值匹配成功执行的语句
break;
default:
如果所有case都没有成功执行default语句
break;
}
public class Demo04 {
public static void main(String[] args) {
// 接收用户的输入
// 1----春天
// 2----夏天
// 3----秋天
// 4----冬天
System.out.println("请输入1,2,3,4");
Scanner num = new Scanner(System.in);
String str = num.nextLine(); //真正接收用户在键盘输入的值
int nu = Integer.parseInt(str);
switch (nu) {
case 1:
System.out.println("春天");
break;
case 2:
System.out.println("春天");
break;
case 3:
System.out.println("春天");
break;
case 4:
System.out.println("春天");
break;
default:
System.out.println("365中某一天");
break;
//所有语句都没匹配成功,会在switch中寻找default,找到就执行反之就不执行
}
}
}
五.三目运算符
结构:判断条件? 值1 : 值2;
public class Demo05{
public static void main(String[] args){
//求三个数中的最大值
int a = 10;
int b = 15;
int c = 20;
int m1 = a > b ? a : b;
int m2 = c > m1 ? c : m1;
System.out.println(m2);//输出:20;
}
}
六while循环
结构:
while(判断条件){
//循环体(一条或多条语句)
}
//当判断条件不成立时循环结束
//打印一百遍"你开心吗?"
public class Demo06{
public static void main(String[] args){
int num = 0;
while(num < 100){
System.out.println("你开心吗?");
num++;
}
}
}
//循环求1-100的和(累加思想)
public class Demo06{
public static void main(String[] args){
int num1 = 1;
int sum = 0;
while(num <= 100){
sum = sum + num1;
num1++;
}
System.out.println(sum);
}
}
//while最简单的死循环
public class Demo06{
public static void main(String[] args){
while(true){
System.out.println("进入死循环");
}
}
}