学Java的简单笔记第一节

Hello world:

package hello;

public class Hello {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		System.out.println("Hello World");
	}
}

输入sys,“ALT+/”,自动出来选项,选中system。选中,再打out,打p,出来个println()

import java.util.Scanner;
Scanner in = new Scanner(System.in);//选中Scanner,系统自动出来这条语句
		System.out.println(in.nextLine());//程序读到了我们的输入

细节:这个地方有个,意思是这个程序已经终止了。
在这里插入图片描述
运行时:在这里插入图片描述
运行结束后,光标是白色。
在这里插入图片描述
这个小地方可以转换进程。
注意:如果程序跑不动了,记得把这里面多余的进程关掉

System.out.println("echo:"+in.nextLine());//用“+”连接两个字符串
System.out.println("2+3="+5);

+:可以连接两个东西
小tips:把两行选中,只要能选中就行,按“Ctrl+/”,可以被注释掉。
解除注释:“Ctrl+/”

System.out.println("2+3="+2+3);//输出23
System.out.println("2+3="+(2+3));//输出5:先算(2+3),再算字符串的连接
System.out.println((2+3)+"=2+3="+(2+3));//输出5=2+3=5//涉及字符的优先级
		System.out.println("100-"+in.nextInt()+"="+(100-in.nextInt()));//用in.nextInt读入我们输入的数字

结果:
在这里插入图片描述
原因:从用户那里读了两次in.nextInt()
解决:
变量

		int price;
		price=in.nextInt();
		System.out.println("100-"+price+"="+(100-price));

变量的规则:标识符只能由字母、数字、下划线组成,数字不能出现在第一个位置。Java关键字不能做标识符。
常量

final int amount=100;//定义为常量不可以修改

浮点数:

package input;

import java.util.Scanner;

public class Main {
    
    

	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		int foot;
		int inch;
		//double inch;
		Scanner in1= new Scanner(System.in);
		foot=in1.nextInt();
		inch=in1.nextInt();
		//inch=in1.nextDouble();
		System.out.println((foot+inch/12.0)*0.3048);//浮点数,只要有一个浮点数,就会按照浮点数来计算
	}
}

注意:

System.out.println(1.2-1.1);

结果:在这里插入图片描述
在计算机里浮点数无法精确计算,当我们需要精确的计算的时候,我们要有整数去进行。
优先级
|-优先级-|-运算符-|-运算-|-结合关系-|-举例-|

优先级 运算符 运算 结合关系 举例
1 + 单目取正 自右向左 a*+b
1 - 单目取负 自右向左 a*-b
2 * 乘法 自左向右 a*b
2 除法 / 自左向右 a/b
2 % 取余 自左向右 a%b
3 + 加法 自左向右 a+b
3 - 减法 自左向右 a-b
3 + 字符串连接 自左向右 "hello"+"bye"
4 = 赋值 自右向左 a=b

猜你喜欢

转载自blog.csdn.net/weixin_46225406/article/details/121338818