Java基础知识总结
Java基础入门
用户交互Scanner
- 基本语法:Scenner s=new Scanner(System.in);
- 通过Scenner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般还需要使用 hasNext() 与hasNextLine() 判断是否还有输入的数据。
import java.util.Scanner;
public class Demo01 {
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流的类一旦不关闭就会一直占用资源 IO流就是输入、输出与电脑相关
scanner.close();
}
}
************************************************************
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
//用于接收字符串类型数据
System.out.println("使用nextLine方式接收:");
//判断是否还有输入
if (scanner.hasNextLine()){
//使用nextLine方式接收
String str=scanner.nextLine();//程序会等待用户输入数据
System.out.println("输出结果为:"+str);
}
//释放资源
scanner.close();
}
}
注意:
next():
- 一定要读到有效字符才会停止输入。
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉。
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
- next()方法不能得到带有空格的字符串
nextLine():
- 以Enter为结束符,也就是说,nextLine()方法返回的是输入回车之前的所有字符。
- 可以获得空白。
顺序结构
JAVA的基本结构就是顺序结构,除非特别指明,否则就是按照顺序一句一句执行。顺序结构是最简单的算法结构。它是任何算法都离不开的基本算法结构。
选择结构
-
if单选择结构
import java.util.Scanner; //if单选泽结构 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("海尔")){ System.out.println(s); } System.out.println("郭牛逼"); scanner.close(); } } -
if双选择结构
import java.util.Scanner; //if双选择结构 public class IfDemo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); if (score>60){ System.out.println("及格"); }else { System.out.println("不及格"); } scanner.close(); } } -
if多选择结构
import java.util.Scanner; //if多选择结构 public class IfDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); if (score==100){ System.out.println("S级"); }else if(score<100 && score>=90){ System.out.println("A级"); }else if(score<90 && score>=80){ System.out.println("B级"); }else if(score<80 && score>=70){ System.out.println("C级"); }else if (score<70 && score>=60){ System.out.println("D级"); }else if (score<60 && score>=0){ System.out.println("E级"); }else { System.out.println("郭牛逼"); } scanner.close(); } } -
嵌套的if结构
-
switch多选择结构
import java.util.Scanner; public class SwitchDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //case穿透 没有break截止 char grade='B'; switch (grade){ case 'A': System.out.println("A优秀"); break; case 'B': System.out.println("B优秀"); // break; case 'C': System.out.println("C优秀"); // break; case 'D': System.out.println("D优秀"); // break; case 'E': System.out.println("E优秀"); // break; default: System.out.println("郭鬼斗"); } scanner.close(); } }
循环结构
-
while循环:先判断后执行
//输出1~100 public class WhileDemo01 { public static void main(String[] args) { int i=0; //只要一直满足i<100成立 就会一直循环下去 while (i<100){ i++; System.out.println(i); } } } //计算1+2+3+...+100=? public class WhileDemo02 { public static void main(String[] args) { int i=0; double sum=0; while (i<=100){ sum=sum+i; i++; } System.out.println("sum="+sum); } } -
do.....while循环:先执行后判断,保证循环体一定会执行一次
//计算1+2+3+...+100=? public class DoWhileDemo01 { public static void main(String[] args) { int i=0; double sum=0; do{ sum=sum+i; i++; }while (i<=100); System.out.println(sum); } } -
for循环:是支持迭代的一种通用结构,是最有效的、最灵活的循环结构。
public class ForDemo01 { public static void main(String[] args) { //for循环 for (int i = 1; i <=100 ; i++) { System.out.println(i); } System.out.println("for循环结束!"); } }
break & continue
break:用于强行退出循环,不执行循环中剩余的语句。
public class BreakDemo {
public static void main(String[] args) {
int i=0;
while (i<=100){
i++;
System.out.println(i);
if (i==20){
break;//强制退出循环体
}
}
}
}
continue:用在循环语句体中,用于终止某次循环过程,就是跳过循环体重尚未执行的语句,接着进行下一次是否执行循环的判定。
public class ContinueDemo {
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+"\t");
}
}
}

浙公网安备 33010602011771号