Maximum Value
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Harbir_Guram
Posted On:   Friday, September 6, 2002 11:12 AM

Can someone please send me any ideas how I can caluculate the maximum number of a value using a for loop Ask user for a number (n) (which will be the number of numbers that will follow) Ask the user n times for another number, and ask you go along remember the maximum value that has been entered and print it out. e.g. How many numbers? 3 Number? 42 Number? 18 Number? 24 Result = 42 So far the code does every thing but does not print the result I have tried using the max method in the the Math Class but it does not work. import javax.swing.JOptionPane; import java.io.*; import java.lang.Math.*; public class Test06{ public s   More>>

Can someone please send me any ideas how I can caluculate the maximum number of a value using a for loop

Ask user for a number (n) (which will be the number of numbers that will follow)

Ask the user n times for another number, and ask you go along remember the maximum value that has been entered and print it out.
e.g.
How many numbers? 3
Number? 42
Number? 18
Number? 24
Result = 42
So far the code does every thing but does not print the result I have tried using the max method in the the Math Class but it does not work.


import javax.swing.JOptionPane;
import java.io.*;
import java.lang.Math.*;

public class Test06{
public static void main (String args[]){

String number = JOptionPane.showInputDialog("How many numbers");

int n = Integer.parseInt(number);
System.out.println("How many numbers? " + number);

for(int i=0; i
String input = JOptionPane.showInputDialog("Number ?");
String input2 = JOptionPane.showInputDialog("Number ?");
int result = Integer.parseInt(input);
int result2 = Integer.parseInt(input2);
System.out.println("Number? " + result);
System.out.println("Result = " + Math.max(result, result2));
}

}
}


Please use html tags to format code blocks.    <<Less

Re: Maximum Value

Posted By:   Jeanne_Boyarsky  
Posted On:   Saturday, September 7, 2002 02:51 PM

You want to read one number in each iteration of the loop, not two. You also need a helper variable to store the maximum value at each iteration and then display it at the end.



p.s. - you want to use the
 tag to format your Java code so that it is easier to read.
About | Sitemap | Contact