Results 1 to 4 of 4
Thread: New To Java Need Help
- 02-12-2011, 04:39 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
New To Java Need Help
I have the following program and I can not get it to work. I am completely new to Java so any help will be greatly appreciated.
The program is to find the largest number of a series entered and compute the frequency. Below is the hint given to me:
// Maintain two variables, max and count.
// max stores the current max number, and count stores its occurrences.
// Initially, assign the first number to max and 1 to count.
// Compare each subsequent number with max.
// If the number is greater than max, assign it to max and reset count to 1.
// If the number is equal to max, increment count by 1.)
Here is what I have so far:
import java.util.Scanner;
public class ValueCounter {
public static void main(String [] args){
// Create a scanner
Scanner input = new Scanner(System.in);
// Read initial data
System.out.print(
"Enter an int value (the program will execute when you press 0): ");
int count = input.nextInt();
// Keep reading data until the input is 0
int max = 0;
//int count = 1;
while (max != 0){
max += count;
// Request the next input from the user
System.out.print(
("Please enter the numbers (The program will terminate when you press 0): "));
count = input.nextInt();
}
//Display results
System.out.print("The largest number is: " + max );
System.out.print("The occurrence of the largest number is " + count);
}
}
- 02-12-2011, 05:35 AM #2
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
int max = 0;<<<<<<
while (max != 0)<<<<<<<<<< check it
ur condition in while is always true coz max is zero.so control never enters in while loop.Last edited by baloda; 02-12-2011 at 05:44 AM.
- 02-12-2011, 05:37 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Here is your code again using code tags. You put [code] at the start of the code and [/code] at the end. That way the code is properly formatted and readable.
Java Code:import java.util.Scanner; public class ValueCounter { public static void main(String [] args){ // Create a scanner Scanner input = new Scanner(System.in); // Read initial data System.out.print( "Enter an int value (the program will execute when you press 0): "); int count = input.nextInt(); // Keep reading data until the input is 0 int max = 0; //int count = 1; while (max != 0){ max += count; // Request the next input from the user System.out.print( ("Please enter the numbers (The program will terminate when you press 0): ")); count = input.nextInt(); } //Display results System.out.print("The largest number is: " + max ); System.out.print("The occurrence of the largest number is " + count); } }
I have the following program and I can not get it to work.
Don't worry about whether the program works or not. We can assume you're posting because it doesn't do what you want. But the thing is, what does happen when you compile and run the code? If it does not compile and you can't understand the compiler's messages copy and post them. If it does compile but does something strange or unwanted happens at runtime, describe the actual behaviour. If you get a runtime error message, post the entire thing (called a stacktrace - it's full of useful information).
- 02-12-2011, 05:53 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
I believe this should do the trick for you. Read through it and see if you understand it.
Also try and use space indents if you are not already
Java Code:import java.util.Scanner; public class ValueCounter { public static void main(String [] args){ // Create a scanner Scanner input = new Scanner(System.in); // Read initial data System.out.print("Enter an int value (the program will terminate when you press 0): "); int i = input.nextInt(); // i, an integer used to hold input from the user // Get the first input as an int from the user int max = 0; // Set max to 0 at the start, as the hint said int count = 1; // Set the count to 1 at the start, as the hint said while (i != 0) { // As long as the input is not 0, keep repeating loop if (i > max) { // If the number given to us by the user is greater than max, set // max to this new number. max = i; count = 1; // Reset count to 1 as your hint said } else count++; // If the number inputed is not greater than max, simply add // to the count of how many occurences of the largest number // Request the next input from the user System.out.print( ("Please enter another number (The program will terminate when you press 0): ")); i = input.nextInt(); // Get input } //Display results System.out.println("The largest number is: " + max ); System.out.println("The occurrence of the largest number is " + count); } }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks