Scanner scanner = new Scanner(System.in)
scanner.hasNext进行判断 下一个是否有元素
scanner.nextLine()全部获取
scanner.next()获取到空字符之前
package demo2; 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()){ String str1 = scanner.nextLine(); String str2 = scanner.next(); System.out.println("输出的内容是"+str1); System.out.println("输出的内容是"+str2); } scanner.close(); } }
if判断语句
if() //判断括号内是否为真如果为真就继续执行
else()//否则执行else中的语句
package demo2; import java.util.Scanner; public class Demo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = 0; float f = 1.0f; if (scanner.hasNextInt()){ i = scanner.nextInt(); System.out.println(i); }else { System.out.println(i+1); } if(scanner.hasNextFloat()){ f = scanner.nextFloat(); System.out.println(f); }else{ System.out.println("not float"); } scanner.close(); } }
while(scanner.hasNextInt())中判断
scanner.hasNextInt()是否输入的为整数
不是则直接结束
while(true)为死循环
package demo2; import java.util.Scanner; public class demo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = 0; int sum = 0; while(scanner.hasNextInt()){ int x = scanner.nextInt(); a = a+1; sum = sum+x; } System.out.println(sum); } }
在while判断前会先进行do操作
没有循环就会执行一次do操作
package demo2; public class Demo07 { public static void main(String[] args) { int a = 0; int sum = 0; while(a<10){ sum = sum+a; a++; } System.out.println(sum); do{ sum = sum+a; a++; }while(a<10); System.out.println(sum); } }
switch是进行多次判断
case如果和switch (grade)括号内grade相同则输出
break为直接退出
如果没有break会击穿
会继续执行下面的case条件
default为默认代表都没有case条件成立时则执行default
package demo2; public class Demo05 { public static void main(String[] args) { int A = 60; int B = 70; int C = 80; int D = 90; int E = 100; char grade = 'c'; switch (grade){ case 'a': System.out.println(A); break; case 'b': System.out.println(A); break; case 'c': System.out.println(A); // break; case 'd': System.out.println(A); // break; case 'e': System.out.println(A); // break; default: System.out.println(E); } } }
Jdk1.7以后switch支持对字符串的比较
package demo2; public class Demo06 { public static void main(String[] args) { String name = "liu"; switch (name){ case "liu": System.out.println('1'); break; case "li": break; } } }
for( ; ; )也合法为死循环
for(参数;判断;条件)
for (int i = 0; i <= 100; i++)
相当于i 从0-100的自增每次加一直到100结束循环
package demo2; public class Demo08 { public static void main(String[] args) { // for(int a=1;a<100;a++){ // System.out.println(a); // } int sum = 0; for (int i = 0; i <= 100; i++) { sum = sum + i; } System.out.println(sum); 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); }
输入0-1000之间能被5整除的数,并且3个为一行
package demo2; public class Demo09 { public static void main(String[] args) { for (int i = 0; i <= 1000; i++) { if(i%5==0){ System.out.print(i+"\t"); } if(i%15==0){ System.out.println(); } } } }
package demo2; public class Demo10 { public static void main(String[] args) { for (int i = 1; i <=9; i++) { for (int j = 1; j <= i; j++) { System.out.print(i + "*" + j + "=" + i * j+"\t"); } System.out.println(); } } }
package demo2; public class Demo12 { 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(""); } } }