With all the Java that I have learned in the 123 pages of "Java for Dummies" so far I feel like a 2 year old kid going through the "Why" stages of their life. So I will post some code that I have a couple of questions about. This was not an excersice from the book but I took different parts from what I understood and wrote my own code. This code add to different dollar amounts and add in good old California sales tax. The code works fine but I want to understand "Why" it is doing somethings instead of copying from the book.
import java.util.Scanner;
public class numbers
{
static Scanner b = new Scanner(System.in);
static public void main(String[] args)
{
System.out.print("Pick an amount ");
double r = b.nextDouble();
System.out.print("Now pick an amount to add ");
double j = b.nextDouble();
System.out.println("With Tax the amount is " + (r + j) * 1.075);
}
}
Now my first question. I created a Scanner class so I could receive input from the user. Now the 'new Scanner(System.in); is being assigned to a variable b' if I understand that right.
Now in the code "Pick an amount" the "b.nextDouble()' takes the the users input and assings that to 'b' which I assign to 'double r'. I also use that same code in the "Now pick an amount to add" "b.nextDouble();".
Since I already assinged the USERS input to 'b' from the first entry I thought that it would need another letter assined to it. Meanning that I would have to code it so it would create 2 different Scanner like this.
static Scanner b = new Scanner(System.in);
static Scanner c = new Scanner(System.in);
So in the code further down it would look like this.
System.out.print("Pick an amount ");
double r = b.nextDouble();
System.out.print("Now pick an amount to add ");
double j = c.nextDouble();
So, "Why" can I assign a variable to 'b' and then assign another value to the same variable, in this case 'b' and have it still work? Does 'b' pass it on to 'r' and then later in the code 'b' passes it on to 'j'
Thanks for the help, I hope I haven't confused you.
-Lars
import java.util.Scanner;
public class numbers
{
static Scanner b = new Scanner(System.in);
static public void main(String[] args)
{
System.out.print("Pick an amount ");
double r = b.nextDouble();
System.out.print("Now pick an amount to add ");
double j = b.nextDouble();
System.out.println("With Tax the amount is " + (r + j) * 1.075);
}
}
Now my first question. I created a Scanner class so I could receive input from the user. Now the 'new Scanner(System.in); is being assigned to a variable b' if I understand that right.
Now in the code "Pick an amount" the "b.nextDouble()' takes the the users input and assings that to 'b' which I assign to 'double r'. I also use that same code in the "Now pick an amount to add" "b.nextDouble();".
Since I already assinged the USERS input to 'b' from the first entry I thought that it would need another letter assined to it. Meanning that I would have to code it so it would create 2 different Scanner like this.
static Scanner b = new Scanner(System.in);
static Scanner c = new Scanner(System.in);
So in the code further down it would look like this.
System.out.print("Pick an amount ");
double r = b.nextDouble();
System.out.print("Now pick an amount to add ");
double j = c.nextDouble();
So, "Why" can I assign a variable to 'b' and then assign another value to the same variable, in this case 'b' and have it still work? Does 'b' pass it on to 'r' and then later in the code 'b' passes it on to 'j'
Thanks for the help, I hope I haven't confused you.
-Lars