0

I have defined 16 variables and assigned them with different kind of values.

int block0 = 5;
int block1 = 6;
int block2 = 8;
int block3 = 25;
int block4 = 8;
int block5 = 23;
int block6 = 2;
int block7 = 1;
int block8 = 6;
int block9 = 4;
int block10 = 5;
int block11 = 7;
int block12 = 15;
int block13 = 4;
int block14 = 5;
int block15 = 8;

How can i find the biggest variable among all using while or for loop?

17
  • 6
    arrays are your friend Commented May 17, 2013 at 0:04
  • Im assuming (from the int) that that's Java and not JavaScript, right? Commented May 17, 2013 at 0:06
  • 1
    Use an array. Question answered already, check here for code : stackoverflow.com/questions/13794225/… Commented May 17, 2013 at 0:06
  • 2
    OK, make them fields in an object and use reflections. Commented May 17, 2013 at 0:09
  • 1
    @JosephtheDreamer Sorting is not that optimal when you only want to find the max. Commented May 17, 2013 at 0:13

4 Answers 4

3

first you have to make them into an array

int[] intArr = new int[16];

int[0] = 5;
int[1] = 6;
...

and then you can iterate

int max = 0;
int maxIndex = 0;

for(int i=0; i<intArr.length; i++)
{
    if(max < intArr[i])
        maxIndex = i;
}
Sign up to request clarification or add additional context in comments.

Comments

3

You should, but if you can't use arrays, you can use Math.max(), like this beautiful code:

int max = Math.max(block0, Math.max(block1, Math.max(block2, Math.max(block3, Math.max(block4, Math.max(block5, Math.max(block6, Math.max(block7, Math.max(block8, Math.max(block9, Math.max(block10, Math.max(block11, Math.max(block12, Math.max(block13, Math.max(block14, block15)))))))))))))));

Unless they are fields in a class, you cannot iterate (using a for or while, as you say) through those variables (local variables). You only option other than Math.max() is a huge if statement. Compared to it, though, Math.max() looks pretty.

Comments

2

If you really can't use arrays, make your own method:

public static int maxValue(int... values) {
        int maximum = Integer.MIN_VALUE;

        for(int x : values)
            maximum = (x > maximum) ? x : maximum;

        return maximum;
    }

That way you can call it with multiple arguments, eg. by saying

int max = maxValue(block0, block1, /*etc*/, block15);

@acdcjunior 's solution with multiple Math.max methods works of course, too.

Comments

0

it's rather easy..

Just do a simple sort..

function findlarge()
{
   var largest = 0;
   for (i = 0; i < 15; ++i)
   {
     largest = ((block+i) > block+(i+1)) ? block+i : block+i+1; 
   }
   return largest;
}

please note you'd need to address the incorrect variable names I used..but this is how I'd sort out the largest..

Good luck :)

4 Comments

You can't do what you seem to think you can do. If i is 1, block+i just adds 1 to block, it does not get you the value of block1.
@Dory In java you can write this code and you will not have an error, but it will not produce what you think
Guys I know this..The tag also said javascript :) and besides please see the note I had bellow..I was just suggesting the logic, not more than that.
But you can't "suggest the logic" if that involves tools that do not exist in the entourage of the solution (as you cannot say "just use eval()"). Now that it is clear that it is only Java related, you should remove this answer.

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.