-1

Can some explain how i'm getting this answer:

three times two = 6

Code:

public class Params1 {
    public static void main(String[] args) {
        String one = "two";
        String two = "three";
        String three = "1";
        int number = 20;
        sentence(one, two, 3);
    }

    public static void sentence(String three, String one, int number) {
        String str1 = one + " times " + three + " = " + (number * 2);
    }

}
5
  • 1
    I really can't see what is to explain. You get what you ask. It would just be easier to read with differently named variables in the function and without the number variable. Commented Jan 7, 2013 at 13:02
  • 1
    Did you really expected 40? Commented Jan 7, 2013 at 13:02
  • what did you expect either? Commented Jan 7, 2013 at 13:03
  • i just need to know why str1 is 'three times two = 6' Commented Jan 7, 2013 at 13:04
  • Its just what you did... see the variables and their values set. Commented Jan 7, 2013 at 13:05

5 Answers 5

6

Here's an useful diagram :

enter image description here

I hope it's clear.

And here's how you could have made the same code less confusing :

    public static void main(String[] args) {
        String a = "three";
        String b = "two";
        sentence(a, b, 3);
    }

    public static void sentence(String a, String b, int number) {
        String str1 = a + " times " + b + " = " + (number * 2);
        System.out.println(str1); // to let you inspect the value
    }
Sign up to request clarification or add additional context in comments.

Comments

4

In the call to the sentence()

sentence(one, two, 3);

just replace, for the arguments sake, all variables with their values:

sentence( "two", "three", 3);

Then have a look what values the parameters inside that function get:

three == "two"
one == "three"
number == 3

Then replace the parameters in the sentence you generate and you have your result!

Besides, your variables names are not really self-explanatory. You should rethink them to prevent such misunderstandings.

2 Comments

And here's the explanation for such variable naming : buildingjavaprograms.com/labs/2012/lab3.shtml#slide11
@JanisKoluzs Some of those exercises look nuts. I don't see the point in confusing the young developers with this. There are so many legitimate difficulties with java (or any language). No need to add this kind of trick.
0

It's really hard to guess what you are trying to achieve here. Surely, you don't expect the "string" literals to be interpreted as actual numeric values...

Correct your variable values:

String one = "one";
String two = "two";

and:

sentence(three, one, 3);

and:

public static void sentence(String three, String one, int number) {
    String str1 = one + " times " + three + " = " + (number);
}

Comments

0

you're giving 3 as a parameter for the 'sentence' method.

sentence(one, two, 3);

change that to

sentence(one, two, number);

Comments

0
String str1 = one + " times " + three + " = " + (number * 2);

here one == two == "three"

and three == one == "two"

and number== 3

so you are getting that out put. Better you refer : JAVA methods help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.