id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

srp4379@lausd.net 1-arraybasics > getLargest2
prev  |  next  |  chance

public int getLargest2(int[] nums)

Given an int[] array with 2 elements
return the largest value.

Create an int return variable called largest.
Assign it a default value, e.g. the first element.
Use an if statement to check if the other value is larger -
and if it is larger, change the value of largest to that value.

public int getLargest2(int[] nums) {
  int largest = nums[0];
  if (nums[1] > largest) {
    largest = nums[1];
  }
  return largest;
}
Note: Another way to check for the larger of two values is to use Math.max():
public int getLargest2(int[] nums) {
  int largest = Math.max( nums[0], nums[1] );
  return largest;
}

getLargest2([5, 10]) → 10
getLargest2([10, 5]) → 10
getLargest2([3, 6]) → 6

...Save, Compile, Run (ctrl-enter)

public int getLargest2(int[] nums) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 1

Copyright Nick Parlante 2017 - privacy