卡码java基础课 | 1. A+B问题I
学习内容:
变量定义
基本数据类型
面向对象
类的定义、属性和方法
new关键字
private和public
static
import关键字
输入:Scanner类的使用
输出语句
while控制多次输入
重点归纳:
1.java程序基本执行结构
Main.java文件
public class Main { //类
public static void main(String[] args){ //程序入口,标准固定写法
;
}
}
Scanner:处理输入数据的标准库
基本使用方式:
Scanner sc = new Scanner(System.in);
sc.close(); // 关闭Scanner对象
nextInt():读取下一个整数。
nextDouble():读取下一个双精度浮点数。
nextLine():读取下一行文本。
hasNext():判断是否还有下一个输入项。如果有,返回 true;否则返回 false。
hasNextInt():判断是否还有下一个整数输入项。
hasNextDouble():检查是否还有下一个双精度浮点数输入项。
3.System类:系统相关的类
System.in:系统输入
System.out:系统输出
例题:
1.题目:
2.解:
点击查看代码
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
sc.close();
}
}