简体   繁体   English

我的扫描仪方法不起作用,while 循环不会中断

[英]My scanner methods aren't working and the while-loop won't break

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.我希望程序做的是允许用户输入一个低于 MAX (1 000 000 000) 但高于 0 的整数。我还使用trycatch方法放置了一个异常块,以便用户无法输入一个字符串。

The exception block works but it also prints the statements inside the if block, which I don't want.异常块有效,但它也会打印if块内的语句,这是我不想要的。

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.我只希望它打印... Please enter a number not a word Try again:在用户输入字符串后。 What I mean is that I want the method's while-loop to break if the user has entered a string.我的意思是,如果用户输入了一个字符串,我希望方法的 while 循环中断。

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.我似乎无法解决的另一个问题是,如果用户输入的整数值超过 MAX (1 000 000 000),程序将继续。 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)正如您所看到的,即使用户输入了一个超过 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.如果您完全不熟悉 Java,我知道 Java 有多么困难。 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:我必须就更干净的代码发表一些评论:

  • method-names in Java always start with a lower-case letter, _ or $ Java 中的方法名总是以小写字母、_ 或 $ 开头
  • avoid global variables if you don't need them如果不需要,请避免使用全局变量
  • avoid duplicate methodes that only differ in their names避免只在名称上不同的重复方法

I hope this code gives you a good start into JAVA.我希望这段代码能给你一个良好的 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM