import java.util.*; public class NumberGames { public static void sortArrays(int[]x, int i) //Method used to help sort the array. { int temp = x[i]; int j = i - 1; while (j >= 0 && temp < x[j]) { x[j + 1] = x[j]; j--; } x[j + 1] = temp; } public static void sortInserting(int[]x, int size) //Method used to insert array values. { for (int i = 1; i < size; i++) { sortArrays (x, i); } } //My problem is here. If the values in the array are similar, multiply that number by a certain number. I have seven different possibilities. I have comparing //two values, but I also have to do, three values, four values, and up to seven values. //I could use a bunch of if else statements, but my code would be longer than the eye can see. LOL! public static double addThemUp(int[]numbers) //Method used to calculate values. { double total = 0; while (numbers[0] == numbers[1]) { total = numbers[0] * 0.1; System.out.println("Total is " + total); } System.out.println(); return total; } public static void main (String[]args) { int[]randomNumbers = new int [7]; //Declare seven numbers array. for (int i = 0; i < 7; i++) { randomNumbers[i] = (int)(7 * Math.random() + 1); //Prints random integers from 1 to 20. } sortInserting(randomNumbers, 7); //Method that calls sortInserting method. System.out.println("Your winning numbers are: "); for (int i = 0; i < 7; i++) { System.out.print(randomNumbers[i] + " "); //Randomly printed numbers. } System.out.println(); addThemUp(randomNumbers); //Method that calls addThemUp method } }

# Comparing and Adding Values in Arrays

Page 1 of 1## 8 Replies - 5266 Views - Last Post: 08 April 2011 - 06:20 PM

### #1

# Comparing and Adding Values in Arrays

Posted 08 April 2011 - 04:41 PM

Hello everybody! I want to compare values in arrays and if they are similar, I want to multiple them by a number without using hundreds of if and else statements. My code is listed below if you all have any ideas.

##
**Replies To:** Comparing and Adding Values in Arrays

### #2

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 04:57 PM

I don't quite understand what you are trying to do. What problem does this solve? You have two numbers, and if one of them is smaller than the other, then you multiply the number with 0.1 and add it to the total varible ?

First of all, you can use /* */ to comment multiple lines .

Second, this will go on in infinity

First of all, you can use /* */ to comment multiple lines .

Second, this will go on in infinity

while (numbers[0] == numbers[1]) { total = numbers[0] * 0.1; System.out.println("Total is " + total); } System.out.println(); return total;

This post has been edited by **CasiOo**: 08 April 2011 - 05:02 PM

### #3

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:10 PM

Sorry if my question was misunderstood. I declared and instiante a random array, (int)(7 * Math.random() + 1). It will print out 7 values. If 2 of the values are similar in the array. I want to multiple that array by .01. If 3 of the values are similar in the array, I want to multiple them by 0.5. I want to do the same thing if the four of the values are similar and more.

This is what I have so far.

This is what I have so far.

public static double addThemUp(int[]numbers) //Method used to calculate values. { double total = 0; for (int i = 0; i < 7; i++) { for(int j = 0; j < i; j++)//Loop used to bar exact numbers from being used. { if(numbers[i] == numbers[j]) { total = numbers[i] * .01; System.out.println("Total " + total); i--; return total; } } } return total; }

### #4

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:41 PM

Sorry if my question was misunderstood. I declared and instiante a random array. It will print out 7 values. If 2 of the values are similar in the array. I want to multiple that array by .01. If 3 of the values are similar in the array, I want to multiple them by 0.5. I want to do the same thing if the four of the values are similar and more.

This is what I have so far.

This is what I have so far.

public static double addThemUp(int[]numbers) //Method used to calculate values. { double total = 0; for (int i = 0; i < 7; i++) { for(int j = 0; j < i; j++)//Loop used to bar exact numbers from being used. { if(numbers[i] == numbers[j]) { total = numbers[i] * .01; System.out.println("Total " + total); i--; return total; } } } return total; }

This post has been edited by **TheGreatOne2k**: 08 April 2011 - 05:42 PM

### #5

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:58 PM

Use nested loops here. for each value in the array, count how many times it repeats in the array.

have a variable counter to count it.

after going over the array use if statements to check the value of that counter. loop the array again and multiply the values if you have to.

have a variable counter to count it.

after going over the array use if statements to check the value of that counter. loop the array again and multiply the values if you have to.

### #6

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:04 PM

japanir, on 08 April 2011 - 05:58 PM, said:

Use nested loops here. for each value in the array, count how many times it repeats in the array.

have a variable counter to count it.

after going over the array use if statements to check the value of that counter. loop the array again and multiply the values if you have to.

have a variable counter to count it.

after going over the array use if statements to check the value of that counter. loop the array again and multiply the values if you have to.

Thank you. I will give it a try.

### #7

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:06 PM

Something like this? What is done is you make an array which goes from 0 to your biggest numbers. Lets say you have a number with the value 5, then you go to your array index 5 and count it up with 1. This you do for each of the numbers in your numbers array.

The problem is if you are working with very big numbers, as your helping array with get to be pretty big!

int total = 0; int countOfNumbers[] = new int[Maximum number]; for ( int i=0; i<numbers.length; i++ ) countOfNumbers[ numbers[i] ]++; for ( int i=0; i<countOfNumbers.length; i++) { if ( countOfNumbers[i] == 2 total += i * 0.01; else if ( countOfNumbers[i] > 2 ) total += i * 0.03; } return total;

The problem is if you are working with very big numbers, as your helping array with get to be pretty big!

This post has been edited by **CasiOo**: 08 April 2011 - 06:10 PM

### #8

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:13 PM

CasiOo, on 08 April 2011 - 06:06 PM, said:

Something like this? What is done is you make an array which goes from 0 to your biggest numbers. Lets say you have a number with the value 5, then you go to your array index 5 and count it up with 1. This you do for each of the numbers in your numbers array.

The problem is if you are working with very big numbers, as your helping array with get to be pretty big!

int total = 0; int countOfNumbers[] = new int[Maximum number]; for ( int i=0; i<numbers.length; i++ ) countOfNumbers[ numbers[i] ]++; for ( int i=0; i<countOfNumbers.length; i++) { if ( countOfNumbers[i] == 2 total += i * 0.01; else if ( countOfNumbers[i] > 2 ) total += i * 0.03; } return total;

The problem is if you are working with very big numbers, as your helping array with get to be pretty big!

Thank you so much. Arrays and I do not get along. Let me see if I can implement it. Thanks again.

### #9

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:20 PM

Duplicated threads merged

Page 1 of 1