I'm currently studying for an exam and I found a little problem with a basic code I wrote to learn methods (I just started, I'm a newbie).
The problem here is that I created a method called test which is supposed to do a simple product between an already stated number and another number decided by the user. I had to declare the variable of the user input as another method because it wouldn't work otherwise, so I created it before everything else and gave it value 0. The problem here is that when I use the method "test", the one which should do the product, it uses the value of the variable I stated in the beginning instead of the one that the user decided.
Here's the code:
import java.util.*;
import java.io.*;
public class ProvaMetodi {
public static int numero; //User's input variable
public static void test(){ //The problematic method
int paolo = 23; //I decided to multiply the user's input by this number
int prodotto = paolo*numero; //Just a product
System.out.println(prodotto); //Tried this, before it was an int class which
//returned the int "prodotto" so i could use it in different scenarios.
}
public static void main (String args []){
Scanner gigi = new Scanner(System.in);
System.out.println("Scrivi un numero e lo moltiplico per 23."); //Just the instructions for the user
int numero = gigi.nextInt();
System.out.println("Numero="+numero); //Added this for debugging purposes. The output is the user's input, so it's correct.
test(); //here's the output, which is always 0.
}
}
int numero = gigi.nextInt();will reassign the class variablenumero, rather than creating a new variable with the same name?