简体   繁体   English

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

[英]My method in Java can't find the value of the variable and sets it as 0. What am I doing wrong?

So I have two methods:所以我有两种方法:

public double calcAvg()
    {

        double dSum;
        dSum=iTest1+iTest2+iTest3/3;
        System.out.print(dSum);
        return dSum;
    }
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;
        }

I am trying to figure out why calcAvg() is setting the values of iTest1, iTest2, iTest3 as 0 after inputting the variables with values like so: Methods.setTestScores(90,78,83);我试图弄清楚为什么calcAvg()在输入具有如下值的变量iTest1, iTest2, iTest3的值设置为 0: Methods.setTestScores(90,78,83);

EDIT Added global code编辑添加了全局代码



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
import java.util.Scanner;
import java.text.DecimalFormat;
public class Proj3
{
    public static void main(String[] args)
    {

        Methods.setTestScores(90,78,83);
        Student MethodAvg = new Student();
        MethodAvg.calcAvg();
        }
}

You didn't post what Methods is, but I'm assuming it's an instance of Student .您没有发布Methods是什么,但我假设它是Student的一个实例。 The reason why iTest1, iTest2, iTest3 are 0 is that you are setting the values in the first Student , but calling calcAvg() on the second one, where the variables have default 0 value. iTest1, iTest2, iTest3为 0 的原因是您在第一个Student中设置值,但在第二个中调用calcAvg() ,其中变量的默认值为 0 。 Try尝试

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

The problem is in the main method, you are setting the variables in instance and calculating the average on a separate instance, try the below code it will work:问题出在 main 方法中,您在实例中设置变量并在单独的实例上计算平均值,请尝试以下代码它将起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载 Java.properties 文件,获取 java.util.MissingResourceException:找不到基本名称的捆绑包我做错了什么? - Loading a Java .properties file, getting java.util.MissingResourceException: Can't find bundle for base name what am I doing wrong? 我的图像去噪方法有什么问题? - What am I doing wrong with my image denoising method? 我的方法需要返回一个字符串,我在做什么错? - My method needs to return a string, what am i doing wrong? 这是一个无限循环吗? 我究竟做错了什么? (Java方法) - Is this an infinite loop? What am I doing wrong? (Java method) 在此接口和方法中,Java泛型在做什么? - What am I doing wrong with Java generics in this interface and method? 我在Java中的For循环在做什么? - What am I doing wrong with my For loop in java? 我的应用程序(Java)怎么了? - What am I doing wrong with my application(Java)? 无法弄清楚我在此方法中做错了什么(compute_even) - Can't figure out what I am doing wrong in this method ( compute_even ) Java Udemy 课程闰年计算器 - 无法弄清楚我做错了什么 - Java Udemy course leap year calculator - can't figure out what am I doing wrong 无法得到这个编译,我做错了什么? - Can't get this to compile, what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM