判断字符串是否相等
package jjava1; import java.util.Scanner; public class a2 { public static void main(String[] args){ Scanner l=new Scanner(System.in); String a=l.nextLine(); if(a.equals("加油加油"))System.out.println("YES"); else System.out.println("NO"); } }
判断输入类型
package jjava1; import java.util.Scanner; public class j1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = 0; float f = 0.0f; System.out.println("请输入整数: "); if (scanner.hasNextInt()) { i=scanner.nextInt(); System.out.println("整数数据"+i); } else{ System.out.println("输入的不是整数数据"); } // Scanner scanner = new Scanner(System.in); // int i = 0; // float f = 0.0f; System.out.println("请输入小数: "); if (scanner.hasNextFloat()) { f=scanner.nextFloat(); System.out.println("小数数据"+f); } else{ System.out.println("输入的不是小数数据"); }scanner.close(); } }
猜数字游戏
/* 实现猜数字的小游戏 */ import java.util.Random; import java.util.Scanner; public class GuessNumber { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("猜数字游戏开始"); System.out.println("输入1-100之间的数据:"); //创建Random类变量 Random ran = new Random(); int ranNumber = ran.nextInt(100)+1; //创建Scanner类变量 Scanner in = new Scanner(System.in); while(true) { int number = in.nextInt(); if(number>ranNumber) { System.out.println("您猜测得数字较大,请继续猜测!"); }else if(number<ranNumber) { System.out.println("您猜测得数字较小,请继续猜测!"); }else { System.out.println("恭喜你,猜对了!"); System.out.println("游戏结束!"); break; } } } }
创建比较函数比较数组最大值
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //动态创建数组 int[] arr = new int[sc.nextInt()];//创建数组大小 for(int i = 0 ; i< arr.length ; i++){ arr[i]=sc.nextInt(); } int max=maxn(arr); System.out.println(max); } public static int maxn(int []arr){ int max=0; for(int i = 0 ; i< arr.length ; i++){ if(max<arr[i])max=arr[i]; } return max; } }
switch多选择结构
单字符
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入等级"); char score=sc.next().charAt(0); //单个字符输入也可用char c=(char)System.in.read(); switch(score){ case 'A':System.out.println("优秀"); break; case 'B':System.out.println("良好");break; case'C':System.out.println("及格"); default:System.out.println("111"); } } }
字符串
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入名字"); String name=sc.nextLine(); //单个字符输入也可用char c=(char)System.in.read(); switch(name){ case "kkk":System.out.println("qqq"); break; default:System.out.println("wwww"); } } }
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { public static void main(String[] args) { int i=0; while(i++<=1000){ if(i%5==0)System.out.print(i+"\t"); if(i%15==0)System.out.println(i); } } }
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { 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(i); } } }
打印九九乘法表
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { public static void main(String[] args) { for(int i=1;i<=9;i++){ for(int j=1;j<=i;j++){ System.out.print(j+"*"+i+"="+(j*i)+"\t"); }System.out.println(); } } }
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { public static void main(String[] args) { for(int i=1;i<=9;i++){ for(int j=1;j<=i;j++){ System.out.print(j+"*"+i+"="+(j*i)+"\t"); }System.out.println(); } } }
增强版for循环
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { public static void main(String[] args) { int [] numbers={10,20,30,40,50,60,70}; for(int x:numbers){ System.out.println(x); } } }
打印三角形
package jjava1; import java.util.Arrays; import java.util.Scanner; public class a3 { public static void main(String[] args) { Scanner kk=new Scanner(System.in); int k=kk.nextInt(); for (int i = 0; i <= k; i++) { for(int j=k;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(); } } }