目录
1,用户交互Scanner
2,顺序结构
3,选择结构
4,循环结构
5,break&continue
6,练习
一,用户交互Scanner
java.util.Scanner是java5的新特性,可以通过scanner类来获取用户的输入
基本语法:
Scanner s=new Scanner(system.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前需要使用hasNext()与hasLine()判断是否还有输入的数据
next与nextline的区别:
next方法不能得到输入时带空格的以后内容(以空格为结束符),nextline可以获得按下enter键前所有内容
public static void main(String[] args) {
//第一步创建一个扫描器的对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收");
//判断用户有没有输入字符串
if (scanner.hasNext()){
//接收next方法接收
String str=scanner.next();
System.out.println("输入的内容为"+str);
}
//凡是属于io流的类如果不关闭就会一直占用资源,养成良好习惯
scanner.close();
}
class practice{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据");
String str2=scanner.nextLine();
System.out.println("您输入的数据为"+str2);
}
}
class practice2{
public static void main(String[] args) {
//Scanner包含许多用法,这里使用不同数据类型来判断是否输入正确。不同数据类型不同的名字
Scanner scanner = new Scanner(System.in);
//从键盘接入数据
int i=0;
float f=0.0f;
System.out.println("请输入整数");
if (scanner.hasNextInt()) {//hasNextInt,判断是不是整数,不是int型,则判断失败
i=scanner.nextInt();
System.out.println("整数数据为"+i);
}else {
System.out.println("你输入的不是整数");
}
System.out.println("请输入小数");
if (scanner.hasNextFloat()) {
f=scanner.nextFloat();
System.out.println("小数数据为"+f);
}else {
System.out.println("你输入的不是小数");
}
scanner.close();
}
}
class practice3{
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++;
sum=sum+x;
System.out.println("你输入了第"+m+"个数据,当前和为"+sum);
}
System.out.println(m + "个数字的和为" + sum);
System.out.println(m + "个数字的平均数为" + sum/m);
scanner.close();
}
}
二,顺序结构
java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句的执行
//顺序结构是最简单的算法结构
//它是任何一个算法都离不开的一种基本算法结构
public class demo1 {
public static void main(String[] args) {
System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("D");//一句一句的顺序执行
}
}
三,选择结构
if语句
if单选泽(双选择)(多选择)结构。
语法
1.
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
2.
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}else{
//为false将执行的语句
}
3.
if(布尔表达式1){
//如果布尔表达式为1将执行的语句
}else if(布尔表达式2){
如果布尔表达式为2将执行的语句
}else if(布尔表达式3){
如果布尔表达式为3将执行的语句
}else{
//为false将执行的语句
}
//1
public class demo1 {
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);
}
System.out.println("end");
scanner.close();
}
}
//2
class practice2{
public static void main(String[] args) {
//考试分数大于60就是及格,小于60就是不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
double score=scanner.nextDouble();
if (score >=60) {
System.out.println("及格了");
} else {
System.out.println("不及格");
}
scanner.close();
}
}
//3
class practice3{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入分数");
double score = scanner.nextDouble();
if (score == 100) {
System.out.println("恭喜你得了满分");
} else if (score >= 80 && score < 100) {
System.out.println("你得到的是良");
} else if (score >= 60 && score < 80) {
System.out.println("你及格了");
} else {
System.out.println("你不及格");
}
scanner.close();
}
}
switch语句
多选择 结构还有一个实现方式就是switch case语句
就是判断一个变量与一系列值中某一个值是否相等,每个值称为一个分支
语法:
switch(表达式){
case value:
//语句
break;//可选
case value:
break;//可选
//你可以有任意个case语句
default: //可选
//语句
}
switch语句中的变量类型可以是:
1,byte,short,int,char
2,从java se7开始,支持string
3,同时case标签必须为字符串常量或者字面量
4,case具有穿透现象,break,不加break则输出匹配一个值后的所有值,所以尽量要加
5,default,当没有可以匹配的值时输出的对象
public class demo2 {
public static void main(String[] args) {
//
char grade='c';
switch(grade){
case'a':
System.out.println("优秀");
break;
case 'b':
System.out.println("良");
case 'c':
System.out.println("及格");
case'd':
System.out.println("不及格");
default:
System.out.println("未知");
}
}
}
上面说了switch语句在jdk7以后支持string,那么来探索一下原因。
IDEA反编译
java----class(字节码文件)-----反编译(idea)
打开project structure,有一个项目编译后输出的路径,打开文件,
会看到class文件,将文件放入idea
为什么switch可以支持字符串,可以看到name变成了name.hashCode,去找的case加一串数字,说明,汉字每个都有一个对应的hash值,通过hash值比较,匹配
四,循环结构
while循环
while是最基本的循环,它的结构为:
while(布尔表达式){
//循环内容
}
- 只要布尔表达式为true,循环就会一直执行下去
- 我们需要一个让表达式失效的方式来结束循环,否则会造成死循环
- 少部分情况需要循环一直执行,比如服务器的请求响应监听
public class demo3 {
public static void main(String[] args) {
//输出1-100
int i=0;
while(i<100){
i++;
System.out.println(i);
}
}
}
计算1+2+3+4+…+100=?
public class demo3 {
public static void main(String[] args) {
int i=0;
int sum=0;
while(i<=100){
sum=sum+i;
i++;
}
System.out.println(sum);
}
}
do…while循环
- while循环如果不满足条件,则不能进入循环,但有时我们需要及时不满足条件,也至少执行一次
- do…while循环就是至少会执行一次
- while先判断后执行,dowhile是先执行后判断
语法:
do{
//代码语句
}while(布尔表达式)
public class demo4 {
public static void main(String[] args) {
int i=0;
while(i<0){
System.out.println(i);
i++;
}
System.out.println("-------------------------");
do {
System.out.println(i);
i++;
} while (i<0);
}
}
for循环*
- 虽然所有的循环结构都可以用while或者dowhile标识,但java提供了另一种for循环,使一些循环节后变得简单
- for循环语句是支持迭代的一直那个通用结构,最是有效,灵活
- for循环执行的次数在执行前就是确定的,语法如下
for(初始化,布尔表达式,更新){
//代码语句
}
(fori,100.for快速生成)
public class demo5 {
public static void main(String[] args) {
int a =1;//初始化条件
while (a<=100) {//条件判断
System.out.println(a);//循环体
a+=2;//迭代=a=a+2
}
System.out.println("while..end----------------");
// 初始化 条件判断 迭代
for (int i = 1; i <=100 ; i++) {
System.out.println(i);
}
System.out.println("for....end-----------------");
}
}
关于for循环小知识:
最先执行初始化步骤,可以声明一种类型,但可以初始化一个或多个循环控制变量,也可以是空语句,然后是检测布尔表达式,如果为true,循环体被执行,为false循环终止,开始执行循环体后面内容
所以,三个都可以省略:
for(;;){
}//死循环
public class demofor1 {
public static void main(String[] args) {
//练习1,计算0到100之间的奇数和偶数和
int oddSum=0;
int evenSum=0;
for (int i = 0; i <= 100; i++) {
if (i%2!=0) {
oddSum=oddSum+i;
}else {
evenSum=evenSum+i;
}
}
System.out.println("奇数的和"+oddSum);
System.out.println("偶数的和"+evenSum);
}
}
public class demofor1 {
public static void main(String[] args) {
//练习2,用while或for循环输出1-1000之间能被5整除的数,并每行输出3个
int oddSum=0;
int evenSum=0;
for (int i = 0; i <= 1000; i++) {
if (i%5==0) {
System.out.print(i+"\t");
}
if (i%(5*3)==0) {
System.out.println("\n");
}
}
}
}
//print输出会换行
//println输出不会换行
public class demofor1 {
public static void main(String[] args) {
//练习3,打印九九乘法表
/*
1,先打印第一列
2,把固定的1再用一个循环包裹
3,去掉重复i<=j
4,调整样式
* */
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <=j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println( );
}
}}
java5引入用于数组的增强for循环
- 先见一面,数组部分重点使用,主要用于数组或集合的增强型for循环
语法:
for(声明语句:表达式){
//代码句子
} - 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配,其作用于限定在循环语句块,其值与此事数组的值相等
- 表达式:表达式是要访问数组名,或者返回值为数组的方法
public class demofor1 {
public static void main(String[] args) {
int arr[]={10,20,30,40};
for (int i = 0; i < 4; i++) {
System.out.println(arr[i]);
}//一样的流程,增强for循环就是偷懒用的
System.out.println("------------------");
for (int x:arr) {
System.out.println(x);
}
}}
break&continue
- break在任何循环语句的主体部分,均可用break控制循环的流程,break用于强行退出循环,不执行循环中的剩余语句,
- continue语句用在循环语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一个是否执行魂环的判定
public class demofor1 {
public static void main(String[] args) {
int i=0;
while (i<100) {
i++;
System.out.println(i);
if (i==30) {
break;
}
}
System.out.println("只是终止循环,还可以执行我哟");
}}
public class demofor1 {
public static void main(String[] args) {
int i=0;
while (i<100) {
i++;
if (i%10==0) {
System.out.println( );
continue;
}
System.out.print(i+" ");
}
}}
结果
练习,打印三角形
可以自己使用debug查看每个流程变化
public class practice {
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();
}
}
}
如果你看到这里,那十分感谢,学的东西很容易忘,而一些细枝末节的小细节方法有很重要,在这里记录,是我自己的笔记,但我又希望别人看的时候有很舒服,因为很多其他地方都不详尽,希望可以帮助到你,