Results 1 to 7 of 7

Thread: Basic Problem

  1. #1
    odd123 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Basic Problem

    I'm just getting into programming, and have a little problem I have been kinda stuck with for ideas.

    I want to prompt a user to enter 10 numbers, and print out which of them is the largest. So far, I have:

    public void calculatorNumber()
    {
    Scanner input = new Scanner(System.in);

    int counter, number1, largest;

    counter = 1;
    number1 = 0;
    largest = 0;


    while(counter <=10)
    {
    System.out.println("Enter number: ");
    number1 = input.nextInt();

    counter++;
    }

    if(counter>10)
    System.out.println("10 Numbers have been entered");
    }

    Obviously this is just the basic skeleton of what I'll be using. I know this could be done using brute force with if statements and such, but is there a way to bypass this?

  2. #2
    javanubby is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Basic Problem

    to be honest, you beat around the bush like a destitute going through trash. I still don't understand exactly WHAT you're having a problem with.
    Quote Originally Posted by odd123 View Post
    I'm just getting into programming, and have a little problem I have been kinda stuck with for ideas.

    I want to prompt a user to enter 10 numbers, and print out which of them is the largest. So far, I have:

    Java Code:
    public void calculatorNumber()
    	{
    		Scanner input = new Scanner(System.in);
    		
    		int counter, number1, largest;
    		
    		counter = 1;
    		number1 = 0;
    		largest = 0;
    		
    		
    		while(counter <=10)
    		{
    			System.out.println("Enter number: ");
    			number1 = input.nextInt();
    			
    			counter++;
    		}
    		
    		if(counter>10)
    			System.out.println("10 Numbers have been entered");
    	}
    Obviously this is just the basic skeleton of what I'll be using. I know this could be done using brute force with if statements and such, but is there a way to bypass this?
    Because so far, you declared 3 variables. used 1 as your counter. another 1 as your 'bank' to save an inputted variable as long as counter is equal or less than 10, OVERWRITING itself every time so it would only end up saving the 10th inputted number. and then it will print "10 numbers have been entered".
    Last edited by javanubby; 03-17-2012 at 10:10 PM.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Basic Problem

    I think you need at least one if statement ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Basic Problem

    Quote Originally Posted by fatabass View Post
    I think you need at least one if statement ?
    Indeed; a language without some form of conditional execution (e.g. an if-statement) isn't a programming language; therefore html and xml aren't programming languages. A language without a form of 'go to' can still be a programming language if it supports some form of loops or if it can handle recursion gracefully. You can hide those if-statements, e.g.

    Java Code:
    maxSoFar= Math.max(maxSoFar, newNumberRead);
    but the if statement is still there: somewhere in the implementation of the Math.max( ... ) method; it just looks a bit cleaner.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    odd123 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Basic Problem

    Ok, so here is my method. I wasn't stuck with anything, just wondering if there is a more intelligible alternative to this:


    public void calculatorNumber()
    {
    Scanner input = new Scanner(System.in);

    int counter, number1, number2, number3, number4, number5, number6, number7, number8, number9, number10, largest;

    counter = 1;
    largest = 0;


    while(counter <=10)
    {
    counter++;

    System.out.println("Enter number: ");
    number1 = input.nextInt();

    System.out.println("Enter number: ");
    number2 = input.nextInt();

    if(number1>number2)
    largest = number1;
    else
    largest = number2;

    System.out.println("Enter number: ");
    number3 = input.nextInt();

    if(number3>largest)
    largest = number3;

    System.out.println("Enter number: ");
    number4 = input.nextInt();

    if(number4>largest)
    largest = number4;

    System.out.println("Enter number: ");
    number5 = input.nextInt();

    if(number5>largest)
    largest = number5;

    System.out.println("Enter number: ");
    number6 = input.nextInt();

    if(number6>largest)
    largest = number6;

    System.out.println("Enter number: ");
    number7 = input.nextInt();

    if(number7>largest)
    largest = number7;

    System.out.println("Enter number: ");
    number8 = input.nextInt();

    if(number8>largest)
    largest = number8;

    System.out.println("Enter number: ");
    number9 = input.nextInt();

    if(number9>largest)
    largest = number9;

    System.out.println("Enter number: ");
    number10 = input.nextInt();

    if(number10>largest)
    largest = number10;

    System.out.println("The biggest number: " + largest);

    break;



    }

    System.out.println("10 Numbers have been entered, you cannot enter more.");
    }

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

    Default Re: Basic Problem

    Have you learnt about arrays. Storing your values in an array and then looping over the array to find the largest would be simplest.

  7. #7
    odd123 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Basic Problem

    Found an easier way to do it:

    public void calculatorNumber()
    {
    Scanner input = new Scanner(System.in);

    int counter, number1, largest;

    counter = 0;
    largest = 0;



    while(counter <3)
    {
    System.out.println("Enter number: ");
    number1 = input.nextInt();
    if(number1>largest)
    largest = number1;
    counter++;
    }
    System.out.println("The biggest number: " + largest);

    }

Similar Threads

  1. Very basic problem with a for loop.
    By Actaeonis in forum New To Java
    Replies: 5
    Last Post: 11-18-2011, 08:22 PM
  2. Basic largest/smallest problem
    By Acesplit in forum New To Java
    Replies: 2
    Last Post: 09-21-2011, 03:24 AM
  3. Very basic java problem
    By mrparker2 in forum New To Java
    Replies: 6
    Last Post: 05-26-2011, 09:50 PM
  4. Replies: 1
    Last Post: 11-18-2010, 09:17 PM
  5. What am I doing wrong in this basic problem
    By Blaedel in forum New To Java
    Replies: 10
    Last Post: 10-11-2009, 08:45 PM

Posting Permissions

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