询问用户是否希望重新运行该程序

时间:2014-02-26 18:44:51

标签: java string methods boolean java.util.scanner

我编写了以下程序,显示一个数字是否为素数,如果不是素数则显示其素因子和下一个素数。

但是,我不知道如何让程序询问用户是否希望输入其他号码。

用户必须使用大小写字母的任意组合回答yesnoyn

如果给出了无效答案,则必须通知程序答案不可接受,然后再次提示其他答案。该程序最多只会提示三次尝试,否则程序将退出。

如果答案为是(以任何允许的形式),程序必须从主函数中的步骤继续。

程序是用原型方法编写的,因为这就是所要求的。

如果有人可以用最后一部分帮助新手,我会非常感激。

代码:

package primefactors;

import java.util.Scanner;

public class Primefactors {
    //------------------three tries check method-------------------

    public static int getNumberWithThreeTries(int m) {

        int count = 1;
        int number;
        String s = "tries";
        while (count <= 3) {
            number = getInputNumber(m); //getScore returns -1 for invalid inputs
            if (number <= 1) {
                if ((3 - count) < 2) {
                    s = "try"; //just make sure that singular /plural form in the next statme is correct
                }
                if (count == 3) {
                    System.out.println("No more tries remaining!\n");
                } else {
                    System.out.println((3 - count) + " " + s + " remaining! Try Again!  \n");
                }
                count = count + 1;
            } else {
                return number;
            }
        }
        return -1;
    }

    //-------------------boolean try again---work in progress---------------
    public boolean askRunAgain() {
        System.out.println("Would u like to solve more problems? ");
        Scanner scanner = new Scanner(System.in);
        boolean askRunAgain = scanner.nextBoolean();

        return askRunAgain;
    }

    //----------------------------------boolen prime check method------------------
    public static boolean isPrime(int m) {

        for (int i = 2; i * i <= m; i++) {
            if (m % i == 0) {
                return false;
            }
        }

        return true;
    }


    //------------------------next prime method-----------------
    private static int nextPrime(int m) {
        if (m % 2 == 0) {
            m = m + 1;
        }

        for (m = m; !isPrime(m); m = m + 2)
                ;

        return m;
    }

    //---------------------primefactors----------------------
    public static String getPrimeFactors(int m) {
        String ans = "";

        for (int i = 2; i <= m; i = i + 1) {

            if (m % i == 0) {
                ans += i + "*";
                m = (m / i);
                i--;
            }
        }
        return (ans.substring(0, ans.length() - 1));
    }

    //----------------------------------------------------------
    public static int getInputNumber(int m) {

        Scanner n = new Scanner(System.in);
        int number = 0;
        System.out.println("Enter an Integer greater than 1");

        if (!n.hasNextInt()) {
            System.out.print("That's not a number! you have ");
            n.next();
            return -1;
        }
        number = n.nextInt();

        if (number <= 1) {
            return -1;
        }

        return number;
    }
    //------------------------------main method ----------------------

    public static void main(String[] args) {

        int number;
        int count = 0;

        number = getNumberWithThreeTries(1);

        if (number <= 1) {
            System.out.println("Program Terminated");
            System.exit(0);
        }

        if (isPrime(number)) {
            System.out.println(number + ": Is a Prime Number \n\n"
                + getPrimeFactors(number) + ":  Is its prime factor");

        } else {
            System.out.println(number + " Is not a Prime number\n\n"
                + getPrimeFactors(number) + " Are its prime factors \n\n"
                + nextPrime(number) + " Is the next Prime number\n ");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要将getNumberWithThreeTries置于while循环中,最后询问用户是否要再次尝试。如果他们说是,那么你的while循环应该继续执行,否则,它应该退出。