Java基础第一课

<span style="font-family: Arial, Helvetica, sans-serif;"></span><div>
</div>

学编程首先养成两个习惯:
1、官网下软件
2、英文读文档、资料、网站
Java 第一个程序:Hello World
语句:system.out.println("Hello World");

package helloworld;

import java.util.Scanner;

public class hellll {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("你好");
//		Scanner in = new Scanner(System.in);
//		System.out.println("echo:" + in.nextLine());
		
		System.out.println("2+3="+5);
	}

}

知识点:println里面“+”做为连接,输出结果:

你好
2+3=5

知识点:ctrl+/ 将代码切换为注释

写程序要习惯所有的事情都用键盘处理,摆脱鼠标,提高效率。


package helloworld;

import java.util.Scanner;

public class hellll {

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

}


输出结果:2+3=23


package helloworld;

import java.util.Scanner;

public class hellll {

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

}

输出结果:2+3=5

package helloworld;

import java.util.Scanner;

public class hellll {

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

}


输出结果:5=2+3=5


package helloworld;

import java.util.Scanner;

public class hellll {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	Scanner in = new Scanner(System.in);
	<span style="color:#ff0000;">int price;</span>
	price = in.nextInt();
	System.out.println("100-"+price+"="+(100-price));
	}

}


变量定义:int price;

<span style="font-size:14px;">package helloworld;

import java.util.Scanner;

public class hellll {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	System.out.println("请输入消费金额:");
	Scanner in = new Scanner(System.in);
	int price;
	price = in.nextInt();
	System.out.println("100-"+price+"="+(100-price));
	}

}
</span>

输出结果:

请输入消费金额:
39
100-39=61


变量定义及赋值:

package helloworld;

import java.util.Scanner;

public class hellll {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("请输入金额");
		Scanner in = new Scanner(System.in);
		<span style="color:#ff0000;">int amount = 100;</span>
		<span style="color:#ff0000;">int price = 0;</span>
		price = in.nextInt();
		<span style="color:#ff0000;">System.out.println(amount + "-" + price + "=" + (amount - price));</span>
				
	}

}


amount 的值为100,是不变的,我们可以把它定义为 常量(final)

package helloworld;

import java.util.Scanner;

public class hellll {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("请输入金额");
		Scanner in = new Scanner(System.in);
		<span style="color:#ff0000;">final int amount = 100;</span>
		int price = 0;
		price = in.nextInt();
		System.out.println(amount + "-" + price + "=" + (amount - price));
				
	}

}

更进一步,amount的值也是读取用户的输入:

package helloworld;


import java.util.Scanner;


public class hellll {


<span style="white-space:pre">	</span>public static void main(String[] args) {
<span style="white-space:pre">		</span>// TODO Auto-generated method stub
<span style="white-space:pre">		</span>System.out.println("请输入面额:");
<span style="white-space:pre">		</span>Scanner in = new Scanner(System.in);
//<span style="white-space:pre">		</span>final int amount = 100;
<span style="white-space:pre">		</span>int amount = 0;
<span style="white-space:pre">		</span>int price = 0;
<span style="white-space:pre">		</span>amount = in.nextInt();
<span style="white-space:pre">		</span>System.out.println("请输入消费金额:");
<span style="white-space:pre">		</span>price = in.nextInt();
<span style="white-space:pre">		</span><span style="color:#ff0000;">System.out.println("收您" + amount + "元,应收" + price + "元,找您" + (amount - price)+"元");</span>
<span style="white-space:pre">		</span>System.out.println(amount + "-" + price + "=" + (amount - price));
<span style="white-space:pre">				</span>
<span style="white-space:pre">	</span>}


}

输出结果:

请输入面额:
50
请输入消费金额:
4
收您50元,应收4元,找您46元
50-4=46


再做优化:

package helloworld;

import java.util.Scanner;

public class hellll {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
				Scanner in = new Scanner(System.in);
//		final int amount = 100;
		int amount = 0;
		int price = 0;
		System.out.print("请输入面额:");
		amount = in.nextInt();
		System.out.print("请输入消费金额:");
		price = in.nextInt();
		System.out.println("收您" + amount + "元,应收" + price + "元,找您" + (amount - price)+"元");
		System.out.println(amount + "-" + price + "=" + (amount - price));
				
	}

}

输出结果:

请输入面额:50
请输入消费金额:44
收您50元,应收44元,找您6元
50-44=6

读输入

Scanner in = new Scanner(System.in);

任何需要读用户输入的程序都需要这行。

读入一行文字

System.out.println(in.nextint());

让in这个对像做读入下一行的动作,交给System.out这个对像去打印一行。

字符串的+

用来连接两个字符串形成更长的

System.out.println("Hello" + “World”);

读整数

price = in.nextint();

要求in这个对像做读入下一个整数的动作,读到的结果赋值给变量price

变量类型

int price = 0

这一行,定义了一个变量,变量的名字是price,类型是int,初始值是0.

Java是一种强类型语言,所有的变量在使用之前必须定义或声明,所有的变量必须具有确定的数据类型。数据类型表示在变量中可以存放什么样的数据,变量中只能存放指定类型的数据。

常量

定义常量的方法:

final amount = 100;

tips

程序要求读入多个数字时,可以在一行输入,中间用空格分开,也可以在多行输入

每次召唤in.nextint(),它就等待用户输入一个整数。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值