英文:
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()
{
//(...)
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论