注意点
- 安装jdk时设置环境变量需要在系统变量中修改,而不是用户变量。
新建变量名 JAVA_HOME,变量值 E:\JDK
修改变量 Path ,在最前面加上 %JAVA_HOME%\bin;
- 属性、方法同面向对象,见下下方代码;
public class Hero {
String name;
float hp;
float armor;
int moveSpeed;
void keng(){
System.out.println("坑队友");
}
float getArmor(){
return armor;
}
void addSpeed(int speed){
moveSpeed = moveSpeed + speed;
}
void lengendary(){
System.out.println("牛逼");
}
float getHp(){
return hp;
}
void recovery(float blood){
hp = hp + blood;
}
public static void main(String[] args){
Hero garen = new Hero();
garen.name = "盖伦";
garen.hp = 616.28f;
garen.armor = 616.28f;
garen.moveSpeed = 350;
garen.addSpeed(100);
System.out.println(garen.name + "当前血量:" + garen.hp );
System.out.println("回血100");
garen.recovery(100);
System.out.println(garen.name + "当前血量:" + garen.hp );
Hero teemo = new Hero();
teemo.name = "提莫";
teemo.hp = 383.f;
teemo.armor = 14f;
teemo.moveSpeed = 330;
}
}
- 计算符号
两边运算单元都为布尔
& 长路与,两侧都会运算
&& 短路与 只要第一个是false,其他的不再计算
长路或 | 短路 || 同上
!取反 ^异或
- 三元运算符
表达式?值1:值2
如果表达式为真 返回值1
如果表达式为假 返回值2
- Scanner读取输入
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int a = s.nextInt();
System.out.println("first:" + a);
int b = s.nextInt();
System.out.println("second:" + b);
String c = s.nextLine();
System.out.println("result:" + c);
float d = s.nextFloat();
System.out.println("result:" + d);
}
}
public class Hero {
String name;
float hp;
float armor;
int moveSpeed;
void keng(){
System.out.println("坑队友");
}
float getArmor(){
return armor;
}
void addSpeed(int speed){
moveSpeed = moveSpeed + speed;
}
void lengendary(){
System.out.println("牛逼");
}
float getHp(){
return hp;
}
void recovery(float blood){
hp = hp + blood;
}
public static void main(String[] args){
Hero garen = new Hero();
garen.name = "盖伦";
garen.hp = 616.28f;
garen.armor = 616.28f;
garen.moveSpeed = 350;
garen.addSpeed(100);
System.out.println(garen.name + "当前血量:" + garen.hp );
System.out.println("回血100");
garen.recovery(100);
System.out.println(garen.name + "当前血量:" + garen.hp );
Hero teemo = new Hero();
teemo.name = "提莫";
teemo.hp = 383.f;
teemo.armor = 14f;
teemo.moveSpeed = 330;
}
}
感谢大海同学(手动滑稽)。