如何使我的方法在另一个方法中查找变量?

huangapple 未分类评论41阅读模式
英文:

How can I get my method to search another method for a variable?

问题

import java.util.Scanner;
import java.io.*;

public class carwip2 {
    static float average_sold = 0F; // Declare average_sold as a static variable accessible throughout the class

    public static void main(String args[]) {
        getAverageCarSales();
        getBonus();
    }

    public static void getAverageCarSales() {
        // ... (existing code remains unchanged)

        average_sold = total_car_sold / (yr_no * 6); // Assign value to the static average_sold variable
        System.out.println("Average number of cars sold per month: " + average_sold);
    }

    public static void getBonus() {
        double bonus = 0;

        if (average_sold > 25) {
            bonus = (yr_no * 6) * (500) * (average_sold - 25);
        }

        bonus = bonus - (bonus * 0.0825);
        System.out.println("The bonus is: " + bonus);
    }

    // ... (rest of the code remains unchanged)
}

To enable the getBonus method to access the average_sold value calculated in the getAverageCarSales method, you need to declare average_sold as a static variable at the class level. This way, it can be accessed by both methods without any issues. In the provided code, I've added the necessary changes to incorporate this modification.

英文:
import java.io.*;
public class carwip2
{
	public static void main(String args[])
	{
		getAverageCarSales();
		getBonus();
	}
	public static void getAverageCarSales()
	{
		Scanner sc= new Scanner(System.in);
		int total_car_sold=0, no_car_sold=0 , z=0;
		int yr_no=0;
		int yrs=0;


		float average_sold=0F;

		System.out.println("Enter number of years");
		yr_no=sc.nextInt();
		if (!isValid(yr_no))
		{

			return;
		}

		for (int i=0; i<yr_no; i++)
		{
			System.out.println("Enter the year");
			yrs=sc.nextInt();
			if (!isValid(yrs))
			{

				return;
			}
			for (int j=0;j<6;j++)
			{
				System.out.println("Enter number of cars sold for year " + yrs + " in month #" + (j+1));
				no_car_sold=sc.nextInt();
				if (!isValid(no_car_sold))
				{

					return;
				}
				total_car_sold=no_car_sold + total_car_sold;
			}
		}
			System.out.println("Total number of months:" + (yr_no*6) );
			System.out.println("Total number of cars sold: " + total_car_sold);
			average_sold = total_car_sold / (yr_no * 6);
			System.out.println("Average number of cars sold per month: " + average_sold);
	}
	public static void getBonus()
	{
		double bonus=0;
		if(average_sold>25)
		{
			bonus=(yr_no * 6) * (500) * (average_sold - 25);
		}
		bonus= bonus-(bonus*.0825);
		System.out.println("The bonus is: " +bonus);
	}
	public static boolean isValid(int x)
	{
		return true;
	}
}

What I'm trying to do with my code is to get my method getBonus to read/find where average_sold is. I've tried putting getBonus(getAverageCarSales), but I wasn't sure what identifier I could use to do so. So, what can I change/incorporate to help my getBonus method find average_sold further up in my coding?

答案1

得分: 0

为什么不将average_sold设为类级别变量!这样该类中的任何方法都可以访问它。

public class carwip2
{
 float average_sold = 0f;
 public static void main(String [] args)
  {
    //your code here
  }
   //rest of your code
英文:

Why don't you make average_sold a class level variable! that way any method in that class can access it.

public class carwip2
{
 float average_sold = 0f;
 public static void main(String [] args)
  {
    //your code here
  }
   //rest of your code

答案2

得分: 0

函数名为getAverageCarSales的函数提供了提示,指明了您需要做什么。返回average_sold的值,并将其作为getBonus的输入参数:

编辑:考虑了两个变量的情况。

public class carwip2
{
    private static float average_sold = 0f;
    private static int yr_no = 0;

    public static void main(String args[])
    {
        getAverageCarSales();
        getBonus();
    }

    public static void getAverageCarSales()
    {            
        //不要在这里初始化average_sold和yr_no。
        //(...)
    }
    
    public static void getBonus() 
    {
        //(...)
    }  
       
}
英文:

The name of the function getAverageCarSales has the hint to what you need to do. Return the value of average_sold and use it as a input parameter of getBonus:

Edit: Changed the code considering both variables.

public class carwip2
{
    private static float average_sold = 0f;
    private static int yr_no = 0;

    public static void main(String args[])
    {
        getAverageCarSales();
        getBonus();
    }

    public static void getAverageCarSales()
    {            
        //dont initialize average_sold and yr_no here. 
        //(...)
    }
    
    public static void getBonus() 
    {
        //(...)
    }  
       
}

huangapple
  • 本文由 发表于 2020年4月4日 09:54:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/61022868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定