Reputation: 87
I am very new to java and have searched around and can't seem to find what to do. I need to take int number
and be able to use it in another method. I have to use two methods to do this. I am unsure how to call upon it.
public static void first()
{
System.out.print("Enter number: ")
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
}
public static void getNumber(String name, int move)
{
if (number == 1)
{
System.out.println("Player shows one" );
}
Upvotes: 2
Views: 13147
Reputation: 22710
Define number as a class attribute.
Something like (its not final/working code)
class myClass{
int number = 3; // Or any other default value
public static void first()
{
//....
obj.number = scan.nextInt();
//...
}
public static void getNumber(String name, int move)
{
if (obj.number == 1)
{
//......
}
}
}
Upvotes: 1
Reputation: 31637
After int number
call method
int number = scan.nextInt();
getNumber("Your Name", number);
Upvotes: 0
Reputation: 48592
Make a method which return a number and call it from another method.
public static int first()
{
System.out.print("Enter number: ")
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
return number;
}
public static void getNumber(String name, int move)
{
int number = first(); //Call method here.
if (number == 1)
{
System.out.println("Player shows one" );
}
}
Upvotes: 2