1. Member
Join Date
Oct 2011
Posts
8
Rep Power
0

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. Member
Join Date
Mar 2012
Posts
8
Rep Power
0

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.
Originally Posted by odd123
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. Senior Member
Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
6

Re: Basic Problem

I think you need at least one if statement ?

4. Re: Basic Problem

Originally Posted by fatabass
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

5. Member
Join Date
Oct 2011
Posts
8
Rep Power
0

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. 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. Member
Join Date
Oct 2011
Posts
8
Rep Power
0

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);

}

Posting Permissions

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