繁体   English   中英

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

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

我是初学者,不知道我的程序有什么问题。

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

异常块有效,但它也会打印if块内的语句,这是我不想要的。

这是控制台打印的内容:

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: 

我只希望它打印... Please enter a number not a word Try again:在用户输入字符串后。 我的意思是,如果用户输入了一个字符串,我希望方法的 while 循环中断。

我似乎无法解决的另一个问题是,如果用户输入的整数值超过 MAX (1 000 000 000),程序将继续。 这是控制台打印的内容。

Number of marbles to divide: 
1000000001
Number of people: 

正如您所看到的,即使用户输入了一个超过 MAX (1 000 000 000) 的整数,程序仍会继续

这是我的代码:

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: ");
        }

        }
    } 
}

如果您完全不熟悉 Java,我知道 Java 有多么困难。 由于我不是那些希望您的问题写得完美并切中要点的人,因此我为您的问题组装了一个解决方案。

我不得不重新组织并稍微压缩一下您的代码。 我必须就更干净的代码发表一些评论:

  • Java 中的方法名总是以小写字母、_ 或 $ 开头
  • 如果不需要,请避免使用全局变量
  • 避免只在名称上不同的重复方法

我希望这段代码能给你一个良好的 JAVA 开端。 玩得开心!

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