importjava.util.Scanner;Scanner input =newScanner(System.in);System.out.println("输入一个数字:");// 判断输入类型if(input.hasNextInt()){// int型int num = input.nextInt();System.out.println(num);}else{System.out.println("不是数字");}
今天是今年的第几天?
publicstaticvoidmain(String[] args){int year =0;int month =0;int day =0;Scanner input =newScanner(System.in);System.out.println("请输入年份:");if(input.hasNextInt()){
year = input.nextInt();}System.out.println("请输入月份:");if(input.hasNextInt()){
month = input.nextInt();}System.out.println("请输入日:");if(input.hasNextInt()){
day = input.nextInt();}int sum =0;for(int i=1; i<month; i++){switch(i){case1:case3:case5:case7:case8:case10:case12:
sum +=31;break;// 跳出switchcase4:case6:case9:case11:
sum +=30;break;case2:// 四年一润,百年不润,四百年再润if(year%4==0&& year%100!=0|| year%400==0){
sum +=29;}else{
sum +=28;}break;}}
sum += day;System.out.println("今年的第"+sum+"天");}
求数组的最大值
publicstaticvoidmain(String[] args){int[] arr1 ={1,2,3,45,36,5,9,7,18,2,0,4};int[] arr2 =newint[10];// 可以装十个// 找数组的最大值int max = arr1[0];for(int i=1; i< arr1.length; i++){// if(arr1[i]>max){// max = arr1[i];// }
max = arr1[i]>max?arr1[i]:max;}System.out.println(max);}