标签:float inpu 提升 ++ 数值 应用 temp please 一个
//Demo1.java
public class Demo1 {
public static void main(String[] args) {
//按照数值非数值分类
//数据类型:数值(整型int 浮点型float、double)
//非数值(字符串String,字符char,布尔boolean(true、false))
//按照基本引用数据类型
//引用数据类型String 其他为基本
int num1 = 10;
//浮点数默认是double
double num2 = 1.0;
float num3 = 5.6f;
System.out.println(num3);
//当表达式中有一个是double类型时。整个结果是double
//强制类型转换 double -> int
//强制去掉小数部分9.8 -> 8
//会造成精度丢失
int num4 = num1 - (int)num2;
System.out.println(num4);
//自动类型转换
//int -> double
num2 = 100;
}
}
规则1. 如果一个操作数为double类型,则整个表达式类型提升为double类型
规则2. 满足自动类型转换的条件
两种类型兼容
目标类型大于源类型
int num4 = num1 - (int)num2;
public class ChangeNumber {
public static void main(String[] args) {
int num1 = 1;
int num2 = 2;
System.out.println("before\tchange:"+ "num1 = "+num1+"\t num2 = "+num2);
int temp = 0;
temp = num1;
num1 = num2;
num2 = temp;
System.out.println("after\tchange:"+ "num1 = "+num1+"\t num2 = "+num2);
}
}
+ - * / % ++ --
+= -= *= /= %=
import java.util.Scanner;
public class num {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner inputScanner = new Scanner(System.in);
System.out.println("Please input a number:");
int no = inputScanner.nextInt();
System.out.println("num is : "+no);
System.out.println(no%10);
System.out.println(no/10%10);
System.out.println(no/100%10);
}
}
== != > < >= <=
&& 与
|| 或
! 非
优先级别:! > && > ||
public class luoji {
public static void main(String[] args) {
/* int x = 8;
int y = 9;
System.out.println((++x==y)&&(++x!=y));//true
System.out.println(x);//10
*/
int x = 8;
int y = 9;
System.out.println((++x!=y)&&(++x==y));//false
System.out.println(x);//9
}
}
三目运算符
? :
import java.util.Scanner;
public class Tiaojian {
public static void main(String[] args) {
//判断一个数是否为偶数(能被2整除的数)
Scanner input = new Scanner(System.in);
System.out.println("请输入一个非零数字");
int num = input.nextInt();
String result = (num%2==0)?"偶数":"奇数";
System.out.println(num + "是" + result);
}
}
标签:float inpu 提升 ++ 数值 应用 temp please 一个
原文地址:https://www.cnblogs.com/adeepbluesky/p/12232877.html