1 import java.util.Scanner; 2 public class Large { 3 4 public static void main(String[] args) { 5 // TODO 自动生成的方法存根 6 Test t=new Test(); 7 8 int length,max; 9 String a=""; 10 System.out.println("请输入数组长度"); 11 Scanner input=new Scanner(System.in) ; 12 length=input.nextInt(); 13 int list[]=new int[length]; 14 for(int i=0;i<length;i++) 15 { 16 System.out.println("请输入"+(i+1)+"个值"); 17 Scanner input1=new Scanner(System.in); 18 19 if(input1!=null&&!input1.equals("")) 20 { 21 list[i]=input1.nextInt(); 22 } 23 else 24 { 25 i--; 26 System.out.println("请不要输入空值"); 27 } 28 29 } 30 max= t.Largest(list,length); 31 System.out.println("最大值为"+max); 32 } 33 34 } 35 class Test{ 36 public int Largest(int list[],int length) 37 { 38 int i,max=list[0]; 39 for(i=0;i<length;i++) 40 { 41 if(list[i]>max) 42 { 43 max=list[i]; 44 } 45 } 46 return max; 47 } 48 }
运行结果:
结果分析:判断string字符串是否为空不能简单的input==null;应
input1!=null&&!input1.equals("")