2 Replies - 9129 Views - Last Post: 28 January 2009 - 04:24 AM Rate Topic: -----

#1 aklo  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 229
  • Joined: 23-January 09

Math.min method. Check arrays for smallest number.

Posted 28 January 2009 - 12:06 AM

Ok i have this project that requires choose either 1,2 or 3.

If i choose choice 1. It will prompt for user name then 5 scores.
All input will be capture to an array.

The program will then loop to show the menu again and now the user will choose choice 2.

When choice 2 is chosen, it will display the name and 5 scores previously input. (I have no problem with this)

Example:

Bowler john's scores were: 10,20,30,40,50
Lowest score is 10
Highest score is 20
Average score is 30

Now i can show the 1st line but i have no idea how to use the math.min and math.max to display the lowest score and highest score.

Example of my code.
I did some research on this math.min /max but i am unable to do it.
also it says that this method can only check for 2 elements problem is i need it to go thru the entire array to sniff out the lowest score.

import java.util.*;
import java.text.*;




public class BowlingProject
{
	static Scanner input= new Scanner(System.in).useDelimiter("\r\n");
	
	static int choice; //menu selection
	static String[] name=new String[10]; //player names
	static int[] scores=new int[10]; //entered scores
	
	public static void main(String[] args) 
	{
		do
		{
		System.out.println("select 1 to enter 5 game scores for a bowler");
		System.out.println("Select 2 to see the lowest, highest, average and total scores of current bowler");
		System.out.println("Select 3 to summarise the scores, announce the winner and exit the program");
		System.out.print("Please select your choice <1-3>: ");
		choice=input.nextInt();
		System.out.println("===========================================================");

	switch (choice)
	{

	case 1 : 
		for (int i=0; i<1; i++)
		{
		System.out.println("Enter Bowler's name: ");
		name[i]=input.next();
		}
		
		System.out.println("Enter 5 scores <0-300>");
		System.out.print("1st game score: ");
		scores[0]=input.nextInt();
			if (scores[0]>300)
				do
				{
				System.out.println("Points CANNOT exceed 300, re-enter...");   
				System.out.print("1st game score: ");
				scores[0]=input.nextInt();		
				}while (scores[0]>300);
	
			 if (scores[0]<=300)
				System.out.print("2nd game score: ");	
				scores[1]=input.nextInt();
			if (scores[1]>300)
				do
				{
				System.out.println("Points CANNOT exceed 300, re-enter...");	
				System.out.print("2nd game score: ");	
				scores[1]=input.nextInt();	
				}while (scores[1]>300);
		
			if (scores[1]<=300)
				System.out.print("3rd game score: " );
				scores[2]=input.nextInt();
			if (scores[2]>300)
				do
				{
				System.out.println("Points CANNOT exceed 300, re-enter...");	
				System.out.print("3rd game score: " );
				scores[2]=input.nextInt();	
				}while (scores[2]>300);
		
			if (scores[2]<=300)
				System.out.print("4th game score: " );
				scores[3]=input.nextInt();
			if  (scores[3]>300)
				do
				{
				System.out.println("Points CANNOT exceed 300, re-enter...");	
				System.out.print("4th game score: " );
				scores[3]=input.nextInt();
				}while (scores[3]>300);
		
			if(scores[3]<=300)
				System.out.print("5th game score: " );
				scores[4]=input.nextInt();
			if (scores[4]>300)
				do
				{
				System.out.println("Points CANNOT exceed 300, re-enter...");
				System.out.print("5th game score: " );
				scores[4]=input.nextInt();
				}while (scores[4]>300);
	
			else 
					System.out.print("");
			break;

	case 2: 
	displayResult(scores, name);  
		break;

	default: 
		System.out.println("Please enter 1,2 or 3 only");
		break;
	
}//end switch

		} while(choice!=3); //end do

}//end of main


static void displayResult(int[] scores, String[] name)
{
int i=0;
	
	System.out.println
		("Bowler " +name[i]+ "'s"+ " scores " + "were "  +scores[0]+ " ," +scores[1]+ " , " +scores[2]+ ", " +scores[3]+ "," +scores[4]  );
		
}//end displayResult


/** static void min(int[0] scores, int[1] scores)
{  
System.out.println(The lowest score is: " +Math.min );
}//end min	this is the part with problem...i have no idea how to do it.**/

}//end class


This post has been edited by aklo: 28 January 2009 - 12:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Math.min method. Check arrays for smallest number.

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Math.min method. Check arrays for smallest number.

Posted 28 January 2009 - 12:20 AM

so spot the minimum just loop through the array
int min = scores[0];
for(int i = 0; i < scores.length; i++)
	if(scores[i] < min)
		min = scores[i];


hope this helps
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,522
  • Joined: 20-September 08

Re: Math.min method. Check arrays for smallest number.

Posted 28 January 2009 - 04:24 AM

	int minScore = Integer.MAX_VALUE;
	int maxScore = Integer.MIN_VALUE;
        for(int i = 0;i < scores.length;i++) {
		minScore = Math.min(scores[i], minScore);
		maxScore = Math.max(scores[i], maxScore);		
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1