java常用结构与简单语法
用户输入Scanner
- Java提供的工具类,用于接受用户输入,java5新特性
练习
//scanner.hasNext()
public class Dome01 {
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);
}
//关闭scanner节省资源,凡是io流不使用就关闭,不然会浪费资源。
scanner.close();;
}
}
//scanner.hasNextLine()
public class Demo02 {
public static void main(String[] args) {
//创建一个扫面对象,接受键盘数据
Scanner scanner=new Scanner(System.in);
System.out.println("输入是");
//判断用户是否输入字符串
if(scanner.hasNextLine()){
String str=scanner.nextLine();
System.out.println("输出为"+str);
}
//关闭scanner节省资源,凡是io流不使用就关闭,不然会浪费资源。
scanner.close();
}
}
- next()
- 读取到有效的字符才可以结束
- 输入有效之前的空白,自动取消
- 不能得到带空格的字符串
- nextLine()
- 以Enter为结束符,接收回车之前的所有字符
- 可以接受空格
Scanner对于int、long等都有具体的支持,使用方法与以上相似。
public class Demo06 {
public static void main(String[] args) {
//输入几个数,算平均数,总和,输入非数字结束输入
int num=0;
double sum=0;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextDouble()) {
if (scanner.hasNextInt()) {
sum = scanner.nextInt()+sum;
num=num+1;
}
}
System.out.println("平均数="+(sum/num+"\n"+"总和="+sum));
scanner.close();
}
}
顺序结构
- java最基本的结构,除非特殊说明不然就是此结构
- 语句从上到下依次进行,任何一个算法都离不开的基本算法结构
public class Demo01 {
public static void main(String[] args) {
System.out.println("hello01");
System.out.println("hello02");
System.out.println("hello03");
System.out.println("hello04");
System.out.println("hello05");
}
}
//从上到下依次进行
选择结构
-
if的单选结构
public class IfDemo01 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("请输入"); String str=scanner.nextLine(); //equals 判断字符串是否相等 //if里面的成立就执行括号里的语句 if(str.equals("hello")){ System.out.println("str"); } scanner.close(); } } //if里面的成立就执行括号里的语句
-
if的双选结构
public class IfDemo02 { public static void main(String[] args) { //考试大于等于60合格,小于60不合格 System.out.println("输入一个分数"); Scanner scanner=new Scanner(System.in); int score=scanner.nextInt(); if(score>=60){ System.out.println("合格"); }else{ System.out.println("不合格"); } scanner.close(); } } //非A及B
-
if的多选结构
-
public class IfDemo03 { public static void main(String[] args) { System.out.println("输入一个分数"); Scanner scanner=new Scanner(System.in); int score=scanner.nextInt(); if(score==100){ System.out.println("满分"); }else if(score>=90){ System.out.println("优秀"); }else if(score>=80){ System.out.println("良好"); }else if(score>=70){ System.out.println("合格"); }else if(score>=60){ System.out.println("及格"); }else{ System.out.println("GG"); } scanner.close(); } }
-
嵌套的if结构
if中嵌套if,查找中常用
-
switch多选结构
- 与剧中的变量类型可以是,int、char、short、bety
- java7之后可以使用String
- case必须是字符串常量或字面量
- 不加break就会穿透,及继续执行之后的代码
- default(可选),输入没有的选择时,执行它。
循环结构
-
while循环
-
do while循环
-
for循环
-
增强for循环
- 用于数组,给x依次赋num的值
public class ForDemo02 { public static void main(String[] args) { int[] num={10,20,30,40,50};//数组 for(int x:num){ System.out.println(x); } System.out.println("========================="); for (int i = 0; i < 5; i++) { int j=num[i]; System.out.println(j); } } //上下两个for含义相同
-
break,用于结束循环语句
-
continue,结束循环中的一轮
-
可以用便签实现goto,但是不建议使用