Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Java方法可以';找不到变量的值并将其设置为0。我做错了什么?_Java_Methods_Instance - Fatal编程技术网

我的Java方法可以';找不到变量的值并将其设置为0。我做错了什么?

我的Java方法可以';找不到变量的值并将其设置为0。我做错了什么?,java,methods,instance,Java,Methods,Instance,所以我有两种方法: public double calcAvg() { double dSum; dSum=iTest1+iTest2+iTest3/3; System.out.print(dSum); return dSum; } 我正在试图弄清楚为什么calcAvg()在输入变量值后将iTest1、iTest2、iTest3的值设置为0,如下所示: Methods.setTestScores(90,78,83

所以我有两种方法:

public double calcAvg()
    {

        double dSum;
        dSum=iTest1+iTest2+iTest3/3;
        System.out.print(dSum);
        return dSum;
    }
我正在试图弄清楚为什么
calcAvg()
在输入变量值后将
iTest1、iTest2、iTest3的值设置为0,如下所示:
Methods.setTestScores(90,78,83)

编辑添加的全局代码



import java.util.Scanner;
import java.text.DecimalFormat;

public class Student
{   Scanner kbAlpha=new Scanner(System.in);
    Scanner kbNum=new Scanner(System.in);
    String strLast;             //student's last name
    String strFirst;            //student's first name
    int iTest1;                 //test 1
    int iTest2;                 //test 2
    int iTest3;                 //test 3
    String strStreet;           //student’s street address
    String strCity;             //student’s city
    String strState;            //student’s state
    String strZip;              //student’s zip


//+Student(first:String,last:String)        //the only constructor
    public String Student(String first , String last)
    {
        return first + last;

    }


//+setName(first:String, last:String):void
    public void setName(String strFirst,String strLast)
{
    if(strFirst.equals(""))
        {

        }
        else
        {
            this.strFirst=strFirst;
        }
        if(strLast.equals(""))
        {

        }
        else
        {
            this.strLast=strLast;
        }
}//end setName (String ,String) 

//+setTestScores( t1:int,  t2:int,  t3:int):void
    public void setTestScores(int iTest1, int iTest2, int iTest3)
    {
        if(iTest1>0)
        {
            this.iTest1=iTest1;
        }
        if(iTest2>0)
        {
            this.iTest2=iTest2;
        }
        if(iTest3>0)
        {
            this.iTest3=iTest3;
        }
    }//end setTestScores(int, int, int)

//+setTest(score:int,numTest:int):void
    public void setTest(int score, int numTest)
    {
        if(numTest>=1 && numTest<=3)
        {
            switch(numTest)
            {
                case 1:
                this.iTest1=score;
            }
        }

    }//end setTest(int, int)

//+setStreet(street:String):void
    public void setStreet(String strStreet)
    {
        if(strStreet.equals(""))
        {
        }
        else
        {
            this.strStreet=strStreet;
        }
    }//end setStreet(String)

//+setCity(city:String):void
    public void setCity(String strCity)
    {
        if(strCity.equals(""))
        {
        }
        else
        {
            this.strCity=strCity;
        }
    }//end setCity(String)

//+setState(state:String):void
    public void setState(String strState)
    {
        if(strState.equals(""))
        {
        }
        else
        {
            this.strState=strState;
        }
    }//end setState(String)

//+setZip(zip:String):void
    public void setZip(String strZip)
    {
        if(strZip.equals(""))
        {
        }
        else
        {
            this.strZip=strZip;
        }
    }//end setZip(String)

//+setAddress(street:String,city:String,state:String,zip:String):void
    public void setAddress(String strStreet,String strCity,String strState,String strZip)
    {
        if(strStreet.equals(""))
        {
        }
        else
        {
            this.strStreet=strStreet;
        }
        if(strCity.equals(""))
        {
        }
        else
        {
            this.strCity=strCity;
        }
        if(strState.equals(""))
        {
        }
        else
        {
            this.strState=strState;
        }
        if(strZip.equals(""))
        {
        }
        else
        {
            this.strZip=strZip;
        }
    }//end setAddress(String,String,String,String)

//+getName():String
    public String getName()
    {
        String strName;
        strName=this.strFirst + this.strLast;
        return strName;
    }//end getName()

//+getTest(numTest:int):int
    public int getTest(int numTest)
    {

        return numTest;

    }//end getTest(int)

//+getAddress():String
    public String getAddress()
    {
        String strAddress;
        strAddress=(this.strStreet + this.strCity + this.strState + this.strZip);
        return strAddress;
    }//end getAddress()

//+getStreet():String
    public String getStreet()
    {
        String strStreet;
        strStreet=this.strStreet;
        return this.strStreet;
    }//end getStreet()

//+getCity():String
    public String getCity()
    {
        String strCity;
        strCity=this.strCity;
        return this.strCity;
    }//end getCity()

//+getState():String
    public String getState()
    {
        String strState;
        strStreet=this.strState;
        return this.strState;
    }//end getState()

//+getZip():String
    public String getZip()
    {
        String strZip;
        strZip=this.strZip;
        return this.strZip;
    }//end getZip()
//+findMax():int
//+findMin():int
//+calcAvg():double
    public double calcAvg()
    {
        //double dAvg;
        double dSum;
        dSum=(iTest1+iTest2+iTest3)/3;
        System.out.print(dSum);
        return dSum;
    }
//+studentRecord():String
    //public String studentRecord()

}
//+letterGrade():char
//+equals(s:Student):Boolean

您没有发布什么是
Methods
,但我假设它是
Student
的一个实例。
iTest1、iTest2、iTest3
为0的原因是您在第一个
Student
中设置值,但在第二个
calcAvg()
中调用,其中变量的默认值为0。试一试

public static void main(String[] args)
{
    Student student = new Student();
    student.setTestScores(90, 78, 83);
    student.calcAvg();
}

问题出在main方法中,您正在实例中设置变量,并在单独的实例上计算平均值,请尝试以下代码,它将起作用:

public static void main(String[] args)
{
    Student std = new Student();
    std.setTestScores(90,78,83);
    std.calcAvg();
}

分享课程全球代码PLZ发布您的完整课程。。。我想,
setTestScores
是一种静态方法,但它并没有反映在您发布的代码中……这绝对不是实际情况。你能给我们看一下全班的情况以及主要的方法吗?这样我们就可以正确地评估出哪里出了问题@我的教授要求我们使用匈牙利符号,否则他会从我们的程序中扣除分数。我很清楚这一点。@GhostCat如果让我猜他为什么让我们使用匈牙利符号,那可能是因为这个人大约60-80岁,从60到70年代就开始编程了。我和其他学生谈过,他们所学的编程风格与我们在他的课堂上所学的完全不同。因为只剩下一个月了,我不得不把这学期剩下的时间都做完了。是的,很抱歉。方法是学生类,Proj3是主类。你的回答帮助我解决了问题。非常感谢你!
public static void main(String[] args)
{
    Student student = new Student();
    student.setTestScores(90, 78, 83);
    student.calcAvg();
}
public static void main(String[] args)
{
    Student std = new Student();
    std.setTestScores(90,78,83);
    std.calcAvg();
}