dimanche 27 septembre 2015

Finding the max number in JAVA

enter code import java.util.Scanner;

public class PartC
{
    public static void main(String[] args)
    {
    Scanner max = new Scanner(System.in);
    boolean done = false;   
    int num = 0;
    int totmax = 0;
    System.out.print("Maximum ('D' to quit)\n");
    while( !done )
    {
        System.out.print("Enter number:");
        if(max.hasNextInt())
        { 
            num = max.nextInt();
        }

        else
        {
            if(num > totmax)
            {
                totmax = num;
            }

            String input = max.next();
            if (input.equals("D"))
            {
                done = true;
                max.close();
                break;
            }
            else
            {   
                System.out.println("Invalid Input -> retry");
            }
        }                       
    }           
        System.out.println("Your Maximum is: " + totmax );

}
}

How do I find the max number when using a scanner and sentinel values? my method only displays the last integer inputted. I've been at this for hours and have put my pride to sleep, I need help....please help..........

Aucun commentaire:

Enregistrer un commentaire