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 18 Replies  3496 Views  Last Post: 08 April 2011  06:20 PM
#1
Comparing and Adding Values in Arrays
Posted 08 April 2011  04:41 PM
Replies To: Comparing and Adding Values in Arrays
#2
Re: Comparing and Adding Values in Arrays
Posted 08 April 2011  04:57 PM
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
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
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
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:
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
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:
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
