I'm a beginner and don't know what is wrong with my program.
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)
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: ");
}
}
}
}
I know how difficult Java can be if you're absolutely new to it. And as I'm not one of those who expect your questions to be perfectly written and brought to the point, I assembled a solution to your problem.
I had to reorganize and squeeze your code a little bit. And I have to make some remarks referring to cleaner code:
I hope this code gives you a good start into JAVA. Have fun!
import java.util.*;
public class MarblesApp
{
private final static int MAX = 1000000000;
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
int numberOfMarbles, numberOfPeople, marblesPerPerson, marblesLeftOver;
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 = getNumberFromConsole();
System.out.println("Number of people: ");
numberOfPeople = getNumberFromConsole();
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.");
}
public static int getNumberFromConsole()
{
int number;
while (true)
{
try
{
// get the number from console
number = input.nextInt();
// validate whether it's greater zero and lower MAX
if(validateNumber(number) == true)
{
// if true, return the number
return number;
}
else
{
// if not, input again
input.next();
}
}
catch (InputMismatchException e)
{
System.out.println("Please enter a number not a word\nTry again: ");
input.next();
}
}
}
private static boolean validateNumber(int number) {
if(number > MAX || number == 0)
{
System.out.println("Please enter a number under 1 000 000 000 and above 0\nTry again: ");
return false;
}
return true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.