1. Member
Join Date
Nov 2012
Location
Caribbean
Posts
4
Rep Power
0

## Control Statement Problem

im having problem executing my app.. can someone please help me get it right. i have no idea what im doing wrong.

Here Is The Question:
(Find the Largest Number) The process of finding the largest value is used frequently in computer
applications. For example, a program that determines the winner of a sales contest would input
the number of units sold by each salesperson. The salesperson who sells the most units wins the contest.
Write a pseudocode program, then a Java application that inputs a series of 10 integers and determines
and prints the largest integer. Your program should use at least the following three variables:
a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been
input and to determine when all 10 numbers have been processed).
b) number: The integer most recently input by the user.
c) largest: The largest number found so far.

Heres my code:
Java Code:
``` public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

int counter;
int number;
int largest;

counter = 1;
number = 0;

while (counter <= 10)
{
System.out.print("Enter amount of units sold:");
number = input.nextInt();
counter = counter + 1;

}

largest = MAX.VALUE.INT(number);
System.out.printf("\nThe maximum Sold Units is: %d\n", largest);```
Last edited by pbrockway2; 11-22-2012 at 12:52 AM. Reason: code tags added

2. ## Re: Control Statement Problem

What is MAX.VALUE.INT? It is not part of the standard API.

Carefully read your code and pretend you are a computer executing it. You have a loop that iterates 10 times. You read in a value and store it in number. Increment counter. Go around the loop again. Read in a second value and store it in number (here's the important bit) overwriting the previous value stored there. Thus the first number is lost. At the end the only value you will have is the last number enetered. How are you supposed to find the highest number? I'll give a hint: you need to compare 2 numbers to see which is the highest and keep it. Repeat that 10 times.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Control Statement Problem

Hi Juice Jay, welcome to the forums!

I've added code tags to your post. When you post code, put [code] at the start of the code and [/code] at the end. That way the code will be formatted properly by the forum software.

---

Perhaps you might have raced too quickly into the code. "MAX.VALUE.INT" isn't Java, and won't fly.

Figure out an algorithm or "plan of attack" for your code. Whether in pseudocode or ordinary language you ought to be able to describe *precisely*, step by step, what the code (when you eventually write it) is going to do.

Take three sheets of paper and label them "counter", "number" and "largest". Have someone tell you some numbers, or roll a dice. What you are attempting to do is figure out the largest of the first ten numbers. You can't depend on your memory, only what you've written on the three sheets of paper. And those can only "hold" one number: that is, when you write a number on one of the pieces of paper you must remove and forget what was there.

It ought to be clear that you can detect when you've been told ten numbers. And that you can say what the largest was. But actually do the exercise, paying critical attention to what you write and when.

It seems to me that you will find yourself updating the "largest" sheet of paper each time you hear a new number. And not be left figuring out what to write all at once after you have heard all the numbers - which is what your code is trying to do.

---

Once you have a plan, post if there are specific questions about how to turn it into Java code.
Last edited by pbrockway2; 11-22-2012 at 01:06 AM.

4. Member
Join Date
Nov 2012
Location
Caribbean
Posts
4
Rep Power
0

## Re: Control Statement Problem

I Did This Now... Still Wont Run As I Expected

import java.util.Scanner;

public class Largest {

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

int number = 0;
int largest;
int counter;

counter = 0;

while (counter <= 1)
{

System.out.print("Enter number of units Sold: ");
number = input.nextInt();
}

largest = number;

while (counter <= 9)
{

System.out.print("Enter number of units sold: ");
number = input.nextInt();
counter++;

if (number > largest)
{
System.out.printf("%d is the largest sale made\n", number);
}

else if (largest > number)
{
System.out.printf("%d is the largest sale recorded\n", largest);
}

}

}
}

5. Member
Join Date
Nov 2012
Location
Caribbean
Posts
4
Rep Power
0

## Re: Control Statement Problem

Thanks For The help tho.. i appreciate it greatly..

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Control Statement Problem

The code isn't readable without the [code] ... [/code] tags. Use the "Edit Post" link beneath where your post appears, and edit the post so that [code] appears at the start of the code and [/code] at the end.

Try, and ask if you have problems doing that.

---

Java Code:
```while (counter <= 1)
{
System.out.print("Enter number of units Sold: ");
number = input.nextInt();
}```
This section of your code has me baffled. Since the code inside the while loop does not change counter at all, the loop will just keep going forever. What was this section of the code supposed to do?

The second while loop is just as mysterious. It will print something most times around the loop - but the question asked you to print just one thing: the overall largest number.

I know I'm repeating myself, but it really would clarify things if, instead of posting code, you actually said (in natural language, or in pseudocode as the problem asks for) how you are attempting to find the largest number.

#### Posting Permissions

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