English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Given two int variables, firstPlaceWinner and secondPlaceWinner , write some code that swaps their values. Declare any additional variables as necessary.

2006-09-01 05:42:51 · 2 answers · asked by ? 1 in Computers & Internet Programming & Design

2 answers

public void switchValue(int firstPlaceWinner, int secondPlaceWinner)
{
int tempWinner = firstPlaceWinner;
firstPlaceWinner = secondPlaceWinner;
secondPlaceWinner = tempWinner;
}

2006-09-01 05:47:27 · answer #1 · answered by jude D 2 · 1 0

The above answer would be correct, except that in the arguements, they declared the variables. By the point you enter this in the program, they would already have variables. If you declare them again, you may lose them. Also, this would probably be a method, not your entire program, so take out the "public static void" declaration and replace it with whatever method you're declaring.

I also think that for whatever you're doing, it's tedious to write a function for swapping first and second place values. Maybe that was just an example of what you were looking for, but if this is what you're doing, I'd like to suggest doing it a different way:

What makes a person advance in places? Points?

If it's points, you can figure out a point system where 1st place = ??? and second place = ??? and so on. Then just declare a variable, playerName.score and compare the value to the point system using if and else if. Then all you have to do is give each player points. If there is a tie, you can either add points to one to place them, or the if and else if function will automatically double the place numbers so it will look like this:

1. Stacie
2. John
2. Billy
3. Chris

This way you'll never have to worry about swapping anything.

Now if you're judging by any other means, I don't know how you've got that in the program, but there's always a way.

I don't see any reason why you'd need to swap a variable with another. Anytime you need variables to swap, though, it's always good to have a reference if and elseif. that way you don't have to worry about confusing yourself with musical variables.

2006-09-01 13:44:29 · answer #2 · answered by Rockstar 6 · 0 0

fedest.com, questions and answers