1.输入与输出java
关于输入与输出,字符串格式化,对应以下:数组
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { // String:我我,int10,float:10.234000,科学计数:1.000000e+05 // %s格式化字符串, %d格式化整型,%f格式化浮点数,%e将浮点数格式化为科学计数法,注意对应的值必须是浮点数 System.out.printf("String:%s,int%d,float:%f,科学计数:%e", "我我", 10, 10.234, 100000.00); System.out.println("你多大?"); Scanner scanner = new Scanner(System.in); // 这就是至关于捕捉系统输入 String name = scanner.nextLine(); // 将捕捉到的信息复制给name,字符串类型 System.out.println("what's your name?"); String age = scanner.nextLine(); System.out.print(name); // println 是print line, 与print区别就是是不是行级输出,这个就是name跟age在一行 System.out.print(age); // 25yang } }
2.if判断spa
if判断基本结构是code
if(条件){...}else if(条件){...}else{...}blog
public class HelloWorld { public static void main(String[] args) { int score = 90; if (score > 90) { // 判断成绩是否大于90 System.out.println("优秀"); } else { // 成绩小于90,走这个 if (score > 80) { // 判断城市是否大于80 System.out.println("良好"); } else if (score > 70) { System.out.println("通常"); } else { if (score > 60) { System.out.println("及格"); } else { System.out.println("不及格"); } } } } }
3.switch语句字符串
switch语句每一个判断条件下必定要加break,不然若是一个条件判断成功,则会执行全部的语句,另外建议最后加一个default,意思是全部都没有断定成功,就执行这个语句块it
public class HelloWorld { public static void main(String[] args) { int score = 90; switch (score){ case 90: System.out.println("90"); break; case 80: System.out.println("80"); break; case 70: System.out.println("70"); break; case 60: System.out.println("60"); break; default: System.out.println("not found"); break; } } }
4.while与do whilefor循环
while是先进行条件判断,而后在执行包裹的语句块,do{}while是先执行包裹的语句块一遍,而后进行条件判断,决定知否继续执行,class
所以“System.out.println("我执行啦");”,这个条件不知足,没有执行,import
虽然score>10也不知足,可是“System.out.println("score等于5");”执行了
public class HelloWorld { public static void main(String[] args) { int score = 90; while (score < 90) { System.out.println("我执行啦"); } while (score > 5) { score--; } System.out.println(score); // 5 do { System.out.println("score等于5"); // score等于5 } while (score > 10); } }
5.for循环
for循环跟js语法结构同样,同时java的for循环也可使用for each操做
public class HelloWorld { public static void main(String[] args) { int[] a = new int[]{1, 2, 3, 4, 5}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); // 1 2 3 4 5 }
// for each for (int i : a) { System.out.println(i); // 1 2 3 4 5 } } }
7.continue与break一个是跳出这次循环继续执行,break是打断本次循环
6. 数组操做:
1).数组循环可使用for循环
2).数组内部元素能够是各类,例如多维数组
public class HelloWorld { public static void main(String[] args) { // 二维数组 int[][] a = new int[2][3]; a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[1][0] = 4; a[1][1] = 5; a[1][2] = 6; for (int[] i : a) { for (int j : i) { System.out.println(j); // 1 2 3 4 5 6 } } } }