1.用户交互Scanner
哎,好难啊。流程控制学的好累
-
J a v a . u t i l . S c a n n e r 是 J a v a 5 的新特征,我们可以通过 S c a n n e r 来获取用户的输入 \textcolor{red}{Java.util.Scanner是Java5的新特征,我们可以通过Scanner来获取用户的输入} Java.util.Scanner是Java5的新特征,我们可以通过Scanner来获取用户的输入(util是一个工具包)
-
基本语法
Scanner s =new Sacnner(System.in);
-
通过Scanner类的next()与nextLine()方法获取输入的字符串。
-
在读取前一般需要使用hasNext()与hasNextLine()判断是否还有输入数据。
-
n e x t ()与 n e x t L i n e ()相当于 C 语言的 s c a n f \textcolor{red}{next()与nextLine()相当于C语言的scanf} next()与nextLine()相当于C语言的scanf
1.1next()的用法
//创建一个扫描器的对象,用于接受键盘数据
//先打new Scanner(System.in);,用alt+enter补全代码 ctrl+d复制当前行到下一行 shift+tab取消缩进
Scanner scanner = new Scanner(System.in);
System.out.println("使用next来接收数据:");
String str = scanner.next();//scanner.next()相当于c语言的scanf
//这里输入hello world
System.out.println("输出的内容为:"+str);//输出为hello. 因为只能读取有效字符,空格为结束
scanner.close();//凡是属于IO流的类都要关闭,不然会一直占用资源
1.2nextLine()的用法
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextline来接收数据:");
String str = scanner.nextLine();//scanner.nextLine()相当于c语言的scanf
//这里输入hello world
System.out.println("输出的内容为:"+str);//输出为hello world. 因为nextLine()是以enter键结束
scanner.close();
1.3 hasNext()的用法
//创建一个扫描器的对象,用于接受键盘数据
//先打new Scanner(System.in);,用alt+enter补全代码
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户是否还有输入,hasNext()有下一个为1
if (scanner.hasNext()){
String str = scanner.next();//输入hello world
System.out.println("输出的内容为:"+str);//输出 hello
}
//凡是属于IO流的类都要关闭,不然会一直占用资源
scanner.close();
1.4 hasNextLine()用法
//创建一个扫描器的对象,用于接受键盘数据
//先打new Scanner(System.in);,用alt+enter补全代码
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextline来接收数据:");
if (scanner.hasNextLine()){//判断有没有下一行
String str = scanner.nextLine();//输入hello world
System.out.println("输出的内容为:"+str);//输出 hello world
}
//凡是属于IO流的类都要关闭,不然会一直占用资源
scanner.close();
1.5 Scanner的进阶用法(输入int 、double等判断)
-
scanner.hasNextInt()判断是否是int 类型
-
scanner.hasNextFloat()判断是否是float 类型
-
int i = scanner.nextInt();输入int类型
-
flaot i = scanner.nextFloat();输入float类型
import java.util.Scanner; public class Demo04 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i=0; float f=0.1F; System.out.println("请输入整数:"); if(scanner.hasNextInt()) //判断输入的是否还有整数 { i = scanner.nextInt(); //输入整数 } else { System.out.println("输入的不是整数数据"); } System.out.println("请输入小数:"); if(scanner.hasNextFloat()) //判断输入的是否还有小数 { f = scanner.nextFloat(); //输入小数 } else { System.out.println("输入的不是小数数据"); } scanner.close();//IO口必须关闭 } }
-
练习:可以输入多个数字,求其综合与平均数,每输入一个数用回车确认,通过输入非数字来结束输入,并执行输出结果
//练习:可以输入多个数字,求其综合与平均数,每输入一个数用回车确认,通过输入非数字来结束输入,并执行输出结果
public class Demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计算输入了多少个数字
int m = 0;
//通过循环判断是否还有输入,并在里面进行每一次求和统计
while(scanner.hasNextDouble())//输入为非数字结束
{
double x =scanner.nextDouble();
m=m+1;//计数
sum = sum+x;
}
System.out.println(m + "个数的和为:" + sum);
System.out.println(m + "个数的和为:" + (sum/m) );
scanner.close();
}
}
2.顺序结构
3.选择结构
3.1 if选择结构
if (判断条件){
//执行语句
} else if (判断条件) {
//执行语句
} else if (判断条件) {
//执行语句
} else {
//执行语句
}
**If语句中学到的附加知识:equals 判断字符串是否相等 **
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals:判断字符串是否相等
if (s.equals("hello"))
{
System.out.println("输出:"+s);
}
else {
System.out.println("不相等");
}
scanner.close();
}
}
3.2 switch语句
switch可以支持字符串
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "狂神";
//Jdk7之后才支持
//反编译: java---calss(字节码文件)---反编译(IDEA中可以反编译)
//反编译是将out文件里的class文件拖到struct目录下就可以
switch (name){
case "狂神":
System.out.println("正确");
break;
case "狂":
System.out.println("错误");
break;
default:
System.out.println("错误");
break;
}
}
}
4.循环结构
4.1while循环
public class WhileDemo01 {
public static void main(String[] args) {
//输出1-100的数
//一直循环while(true)
int i=0;
while(i<1)
{
i++;
System.out.println(i);
}
}
}
4.2 do…while循环
//do while循环,先执行在判断
public class WhileDemo02 {
public static void main(String[] args) {
int i=0;
int sum=0;
do {
sum=sum+i;
i++;
}while(i<10);
System.out.println(sum);
}
}
4.3 for循环
- 在JAVA中有快捷键 100.for自动生成
- 也有自动生成 fori
public class ForDemo01 {
public static void main(String[] args) {
//快捷键 100.for自动生成
//或者fori
for (int i = 0; i < 100; i++){
System.out.println(i);
}
}
}
/*初始化时可以一个初始化,也可以多个初始化。更可以为空
for( ; ; )也是一种死循环写法
{
}
*/
- 将System.out.println();输出之后会换行
- System.out.print();输出之后不会换行
//练习 用for循环输出1-1000之间能被5整除的数,并且每行输出3个
//将System.out.println();输出之后会换行
// System.out.print();输出之后不会换行
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
if (i%5==0){
//将System.out.println();改成System.out.print();
System.out.print(i+"\t");
}
//换行
if (i%(5*3)==0)//说明每行输出三个就。 这边也可以用count计数器
{
System.out.println();
//或者System.out.print("\n");
}
}
}
}
- 增强for循环,作用在数组之中的
//增强for循环
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};//定义一个数组
//java增强遍历数组
for (int x:numbers){
System.out.println(x);//输出数组
}
System.out.println("========================");
//传统遍历数组
for(int i=0;i<5;i++)
{
System.out.println(numbers[i]);//输出数组
}
}
}
5. break、continue与goto
-
goto不用掌握
-
联系打印三角形
-
//打印三角形 public class TestDemo01 { public static void main(String[] args) { for (int i =1; i <= 5; i++) { for (int j =5; j >=i; j--) { System.out.print(" "); } for (int j =1; j<=i; j++) { System.out.print("*"); } for (int j =1; j<i; j++) { System.out.print("*"); } System.out.println(); } } } /* * *** ***** ******* ********* */