dimanche 29 janvier 2017

Please Help! My scanner methods aren't working and the while-loop won't break

Sorry, I'm just a beginner and don't really know what is wrong with my program. I haven't got a clue what I am supposed to do.

What I would like the program to do is to allow the user to enter an Integer that is under MAX (1 000 000 000) but above 0. I also have put an exception block using the try and catch methods so that the user cannot enter a String.

The exception block works but it also prints the statements inside the if block --which I don't want.

This is what the console prints:

Number of marbles to divide: 
*three*
Please enter a number not a word
Try again: 
Please enter a number under 1 000 000 000 and above 0
Try again: 

I only want it to print... Please enter a number not a word Try again: after the user enters a String. What I mean is that I want the method's while-loop to break if the user has entered a string.

Another problem that I can't seem to solve is that if the user enters an Integer that's value is over MAX (1 000 000 000), the program continues. This is what the console prints...

Number of marbles to divide: 
1000000001
Number of people: 

As you can see the program continues even though the user has entered an Integer over MAX (1 000 000 000)

I do not know what to do, please help!

This is my code...

import java.util.*;

public class MarblesApp
{
    final static int MAX = 1000000000;
    static int  numberOfMarbles;
    static int numberOfPeople, marblesPerPerson, marblesLeftOver;
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) 
    {
        System.out.println("Welcome to the marble divvy-upper.");
        System.out.println("This program will tell you how many marbles to give to each person.\n"
    + "The maximum amount of marbles is 1 000 000 000. The maximum amount of people is the same.\n");

        System.out.println("Number of marbles to divide: ");
          numberOfMarbles = GetMarbles();

        System.out.println("Number of people: ");
          numberOfPeople = GetPeople();

        marblesPerPerson = (int)numberOfMarbles / numberOfPeople;
        marblesLeftOver = (int)numberOfMarbles % numberOfPeople;

        System.out.println("Give each child " + marblesPerPerson + " marbles."); 
        System.out.println("You will have " + marblesLeftOver + " marbles left over.");
    }
private static int GetPeople()
{
    while (true)
    {
        try
        {
            return input.nextInt();
        }
        catch(InputMismatchException f)
        {
            input.next();
            System.out.println("Please enter a number not a word\nTry again: ");
        }

        if(numberOfPeople > MAX || numberOfPeople == 0);
        {
            System.out.println("Please enter a number under 1 000 000 000 and above 0\nTry again: ");
        }
    }
}
public static int GetMarbles()
{
    while (true)
    {
        try 
        {
            return input.nextInt();
        }
        catch (InputMismatchException e)
        {
            input.next(); 
            System.out.println("Please enter a number not a word\nTry again: ");
        }

        if(numberOfMarbles > MAX || numberOfMarbles == 0);
        {
            System.out.println("Please enter a number under 1 000 000 000 and above 0\nTry again: ");
        }

        }
    } 
}

Aucun commentaire:

Enregistrer un commentaire