import java.util.*;public class AnotherCalculation{    public static void main(String[] args){
    
        Scanner keyboard=new Scanner(System.in); int max=0,min=0,next=0,total=0; int count=1; System.out.println("Enter a exam  of student's"); System.out.println("And enter a negative number while you want to stop inputing scores:"); int firstScore=keyboard.nextInt(); if(firstScore>=0){

    max=firstScore; min=firstScore; total=firstScore;

}else{System.exit(0);} System.out.println("Continue inputing another score:"); next=keyboard.nextInt(); while(next>=0){

    if(next>max){max=next;} if(next<min){min=next;} total=total+next; count++; System.out.println("Next score:"); next=keyboard.nextInt();

}//while loop ends here. double average=total/count; System.out.println("The highest score is: "+max); System.out.println("The lowest score is: "+min); System.out.println("The average score is: "+average);
    
    }//main method ends here.}//class ends here.请教各位高手,为什么每次初始化max、min和total的时候都要赋值?如果没有赋值则不能通过编译。纵观整段代码,如果firstScore为正,那么max、min和total会被赋予firstScore的值;如果firstScore为负,那么程序也会直接退出,用不到最后的屏幕输出,为什么编译器非要我初始化的时候赋值,不能单单只是声明变量?希望能有较为详细的回答,十分感谢。

解决方案 »

  1.   

    public class Test1 {
                  
       public static void main(String[] args) {
           int i,j;
               
         System.out.println("helloworld");     
       }
    }
    输出helloworldpublic class Test1 {
                  
       public static void main(String[] args) {
           int i,j,z;
           z=i+j;   //报错,尚未初始化i和j
               
         System.out.println(z);     
       }
    }
    明白了吗??
      

  2.   

    import java.util.*;public class AnotherCalculation{    public static void main(String[] args){
        
            Scanner keyboard=new Scanner(System.in);        int max,min,next,total;        int count=1;        System.out.println("Enter a exam  of student's");        System.out.println("And enter a negative number while you want to stop inputing scores:");        int firstScore=keyboard.nextInt();        if(firstScore>=0){
            
                max=firstScore;            min=firstScore;            total=firstScore;
            
            }else{System.exit(0);}        System.out.println("Continue inputing another score:");        next=keyboard.nextInt();        while(next>=0){
            
                if(next>max){max=next;}  //这里对局部变量进行了操作,所以要赋值            if(next<min){min=next;}//这里对局部变量进行了操作,所以要赋值            total=total+next;//这里对局部变量进行了操作,所以要赋值            count++;            System.out.println("Next score:");            next=keyboard.nextInt();
            
            }//while loop ends here.        double average=total/count;//这里对局部变量进行了操作,所以要赋值        System.out.println("The highest score is: "+max);        System.out.println("The lowest score is: "+min);        System.out.println("The average score is: "+average);
        
        }//main method ends here.}//class ends here.
      

  3.   


    if(firstScore>=0){
            
                max=firstScore;//这里不是已经对max赋值了吗?            min=firstScore;//难道这里也不是赋值吗?            total=firstScore;
            
            }
      

  4.   

    你这里是if,编译器也就会认为不一定执行里面的句子,虽然你在else时退出了,但编译器没有那么聪明的,所以会报错!!
      

  5.   

    其实是因为编译器比较聪明才要求你必须赋值的,java设计时出于安全的考虑,避免很多因为没有初始化而造成的问题。
    比如在c/c++中,你不初始化编译器也让你通过,但是你读取的时候会读到一个随机的值,这就给程序带来一些不确定的问题。