Results 1 to 4 of 4
  1. #1
    Tamtome is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Unhappy Need help with array

    Hey!

    Im having a problem with an assignment , i need to make an array that stores the users input (20 numbers) and then prints out the averege number, the smallest number, the largest number and then all the 20 numbers that the user put in but (5 numbers each line)

    I dont know how to do that, my code only shows the 20 numbers. Can somebody please help me with this?

    code:

    Java Code:
    import java.util.*;
    public class Laboration6 {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner (System.in);
            int[] num = new int [21];
                for (int i = 0; i < num.length; i++)
                {
                    System.out.print("Enter Value " + (i+1) + ": ");
                    num[i] = scanner.nextInt();
                }
                
                System.out.print("You have entered: ");
                
                for(int n: num)	
                {
                	System.out.print(n + " ");
                }
                    
        }
    
    }
    Moderator Edit: code tags added
    Last edited by Fubarable; 01-16-2011 at 02:59 PM. Reason: Moderator Edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You will need to add some new variables to your code, for instance, I'd create variables with names like largestNumber, smallestNumber, totalNumber. Then as you have the user create the array, in the for loop, use these variables to get what you want. Set the totalNumber, largestNumber and smallestNumber = to the first number entered (you'll need an if block for this). Then as new numbers are enterered add them to the totalNumber variable, and compare the new number to the current largestNumber and if bigger, make the largestNumber = the new number, and like-wise with the smallest number.

    Also, please read my link below on how to use code tags which will make it easier for us to be able to help you.


    Luck!

  3. #3
    Tamtome is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the help! im still a bit confused because im new and i dont really get how things work, now i can get the smallest, largest and the average number but i dont know how to connect it with my first code, how can i show all the 20 numbers and 5 numbers each line?

    Java Code:
    import java.util.*;
    public class ScanArray {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner (System.in);
            int[] num = new int [21];
          
            
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("Skriv in 20 tal och avsluta med ett negativt tal .");
    //enter 20 numbers and end with a negative number
            int count = 0, maxInteger = 0, minInteger = -1, sum = 0;
    
            while(keyboard.hasNextInt())
    
            {
    
                int n = keyboard.nextInt();
    
                if(n < 0) break; 
    
                if(minInteger == -1) minInteger = n; 
    
                sum += n; count++; 
    
                if(n > maxInteger) maxInteger = n; 
    
                if(n < minInteger) minInteger = n; 
    
            }
    
            System.out.println("det största numret är: " + maxInteger);
    
            System.out.println("Det minsta numret är: " + minInteger);
    
            System.out.println("Medelvärdet är: " + (sum/count));
            System.out.println("Du har skrivit in" + num.length);
    
    
            	
            
            for (int i = 0; i < num.length; i++)
                {
                    System.out.print("Enter Value " + (i+1) + ": ");
                    num[i] = scanner.nextInt();
                }
                
                System.out.print("You have entered: ");
                
                for(int n: num)	
                {
                	System.out.print(n + " ");
                }
                    
        }
    
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Now is the time to expand your knowledge and learn how to use methods. You have several tasks (highest, lowest, average etc). Place each one of these tasks in a separate method and call them one at a time.

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •