I am trying to find away to get the user to input the number of cannonballs/ammo
ID: 639955 • Letter: I
Question
I am trying to find away to get the user to input the number of cannonballs/ammo they would like to use. The assignment requires the "ammo" to be a static variable. Please Help!
import java.util.Scanner;
public class Cannon{
private static int ammo;
public static void setAmmo(int myAmmo)
{
System.out.println("Enter the number of Cannon Balls:");
Scanner in = new Scanner(System.in);
myAmmo=in.nextInt();
ammo=myAmmo;
System.out.println("The amount of ammo is now : " + ammo);
}
}
This is the error I get when i try to call the method from another class.
error: non-static method setAmmo(int) cannot be referenced from a static context
Cannon.setAmmo(0);
^
Explanation / Answer
import java.util.Scanner;
public class Cannon{
private static int ammo;
public static void setAmmo(int myAmmo)
{
System.out.println("Enter the number of Cannon Balls:");
Scanner in = new Scanner(System.in);
myAmmo=in.nextInt();
ammo=myAmmo;
System.out.println("The amount of ammo is now : " + ammo);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.