程序开始时程序停止(我找不到支架问题)

时间:2016-07-07 16:04:55

标签: java

我的程序在

之后立即停止
System.out.println("Would you like to calculate simple interest or compound interest?"); 

userInputStr = myScanner.next();

并且没有我能找到的支架问题。我知道它与括号有关但我找不到它。有什么建议吗?

这是我的代码(稍后我将实现方法)

  import java.util.Scanner;

public class Interest

{

    static double userInputDou;

    static String userInputStr;

    static double rate;

    static double time;

    static double principal;

    static double interest;

    static double subinterest;

    static double cNum;

    static double total;

    static Scanner myScanner = new Scanner(System.in);

    public static void main(String[] args)

    {

        System.out.println("Would you like to calculate simple interest or compound interest?");

        userInputStr = myScanner.next();

        if (userInputStr.equalsIgnoreCase("simple interest"))

        {

            System.out.println("Please enter the rate: ");

            if (myScanner.hasNextDouble())

            {

                userInputDou = myScanner.nextDouble();

                rate = userInputDou;

            }

            else

            {

                System.out.println("Please enter a integer for the rate.");

            }

            System.out.println("Please enter the time: ");

            if (myScanner.hasNextDouble())

            {

                userInputDou = myScanner.nextDouble();

                time = userInputDou;

            }

            else

            {

                System.out.println("Please enter an integer for the time.");

            }

            System.out.println("Please enter the principal: ");

            if (myScanner.hasNextDouble())

            {

                userInputDou = myScanner.nextDouble();

                principal = userInputDou;

            }

            else

            {

                System.out.println("Please enter an integer for the principal.");

            }

            interest = (principal * rate * time);

            System.out.println("Your interest is $" + interest);

            System.out.println("Your total payment is $" + (interest + principal));

        }

        else if (userInputStr.equalsIgnoreCase("compound interest"))

        {

            System.out.println("Please enter the rate: ");

            if (myScanner.hasNextDouble())

            {

                userInputDou = myScanner.nextDouble();

                rate = userInputDou;

            }

            else

            {

                System.out.println("Please enter a integer for the rate.");

            }

            System.out.println("Please enter the time: ");

            if (myScanner.hasNextDouble())

            {

                userInputDou = myScanner.nextDouble();

                time = userInputDou;

            }

            else

            {

                System.out.println("Please enter an integer for the time.");

            }

            System.out.println("Please enter the principal: ");

            if (myScanner.hasNextDouble())

            {

                userInputDou = myScanner.nextDouble();

                principal = userInputDou;

            }

            else

            {

                System.out.println("Please enter an integer for the principal.");

            }

            System.out.println("Would you like to compound the interest quarterly or annually?");

            userInputStr = myScanner.next();

            if (userInputStr.equalsIgnoreCase("quarterly"))

            {

                System.out.println("For how many quarters would you like to compound the interest for?");

                if (myScanner.hasNextDouble())

                {

                    userInputDou = myScanner.nextDouble();

                    cNum = (userInputDou * 4);

                    subinterest = (principal * (1 + (rate / cNum)));

                    interest = Math.pow(subinterest, time);

                    total = (principal + interest);

                    System.out.println("Your quarterly compounded interest is $" + interest);

                    System.out.println("Your total payment is $" + total);

                }

                else

                {

                    System.out.println("Please enter an integer for the number of quarters.");

                }

            }

            else if (userInputStr.equalsIgnoreCase("annually"))

            {

                System.out.println("For how many years would you like to compound the interest for?");

                if (myScanner.hasNextDouble())

                {
                    userInputDou = myScanner.nextDouble();

                    cNum = userInputDou;

                    subinterest = (principal * (1 + (rate / cNum)));

                    interest = Math.pow(subinterest, time);

                    total = (principal + interest);

                    System.out.println("Your annually compounded interest is $" + interest);

                    System.out.println("Your total payment is $" + total);

                }

                else

                {

                    System.out.println("Please enter an integer fro the number of years.");

                }

            }

            else

            {

                System.out.println("Please enter either 'quarterly' or 'annually'.");

            }

        }

    }

}

3 个答案:

答案 0 :(得分:1)

userInputStr = myScanner.next()等待来自控制台的输入。你需要输入内容。阻塞是很正常的。

根据您输入的评论"简单"然后输入了。我可以说这个字符串"简单"你没有做过任何顶级比较" userInputStr.equalsIgnoreCase("简单兴趣")"和"否则if(userInputStr.equalsIgnoreCase("复利"))"

,程序终止。你需要键入"简单兴趣"或者"复利"看到任何进一步的活动。

答案 1 :(得分:1)

myScanner.next()只会获得一个令牌,这意味着它会获得该行中的第一个单词,但不会获得多个单词,并且您要检查多个单词:< / p>

if(userInputStr.equalsIgnoreCase("simple interest"))

例如,如果你运行它:

    System.out.println("Would you like to calculate simple interest or compound interest?");
    userInputStr = myScanner.next();

    System.out.println(userInputStr); // !!

如果用户输入&#34;简单兴趣&#34;你会看到userInputStr只包含一个单词,#34;简单&#34;。因此,您的整个应用程序将退出,因为最基本的应用程序是这样的:

public static void main(String[] args) {
    System.out.println("Would you like to calculate simple interest or compound interest?");
    userInputStr = myScanner.next();

    System.out.println(userInputStr); // !!

    if (userInputStr.equalsIgnoreCase("simple interest")) {
        // .... do some stuff
    } else if (userInputStr.equalsIgnoreCase("compound interest")) {
        // .... do other stuff
    }
}

因此,如果userInputStr可能与两个字的字符串匹配,那么应用程序将只是退出。

这种方法的一种方法是使用myScanner.nextLine(),因为这将获得整个用户输入行。

所以改变:

userInputStr = myScanner.next();
if(userInputStr.equalsIgnoreCase("simple interest"))

为:

userInputStr = myScanner.nextLine();
if(userInputStr.equalsIgnoreCase("simple interest"))

但请注意,这会增加另一个问题,因为当nextLine()myScanner.next() myScanner.nextDouble()时,myScanner.nextInt()还会获得任何未被吞噬的行尾令牌。除了myScanner.nextXxx()之外,还有myScanner.nextLine()或大多数myScanner.nextDouble()。因此,每次拨打myScanner.nextLine()时,请立即拨打userInputDou = myScanner.nextDouble(); rate = userInputDou; 以吞下行尾令牌。

因此,例如,改变:

userInputDou = myScanner.nextDouble();
myScanner.nextLine(); // swallow the end-of-line token
rate = userInputDou;

成:

import java.util.Scanner;

public class TestScanner {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        System.out.print("Please enter a multi-word sentence: ");
        String input = myScanner.next();
        System.out.println("Output: " + input);

        myScanner.close();
    }
}

为了亚历山大的利益,请运行此代码并输入&#34; Hello World&#34;

$('body').on('click', '.dashboard_leftNav_category a', function() {
      var link = $(this).attr('showSection'); //changed from let link
      var show = $('[section="'+link+'"]');
      $('[section]').hide();
      $('body').find(show).fadeIn();
      $('html,body').scrollTop(0);
    });

答案 2 :(得分:0)

您应该使用nextLine()而不是next()的扫描仪。

import java.util.Scanner;

公共课堂考试

{

static double userInputDou;

static String userInputStr;

static double rate;

static double time;

static double principal;

static double interest;

static double subinterest;

static double cNum;

static double total;

static Scanner myScanner = new Scanner(System.in);

public static void main(String[] args)

{

    System.out.println("Would you like to calculate simple interest or compound interest?");

    userInputStr = myScanner.nextLine();

    if (userInputStr.equalsIgnoreCase("simple interest"))

    {

        System.out.println("Please enter the rate: ");

        if (myScanner.hasNextDouble())

        {

            userInputDou = myScanner.nextDouble();

            rate = userInputDou;

        }

        else

        {

            System.out.println("Please enter a integer for the rate.");

        }

        System.out.println("Please enter the time: ");

        if (myScanner.hasNextDouble())

        {

            userInputDou = myScanner.nextDouble();

            time = userInputDou;

        }

        else

        {

            System.out.println("Please enter an integer for the time.");

        }

        System.out.println("Please enter the principal: ");

        if (myScanner.hasNextDouble())

        {

            userInputDou = myScanner.nextDouble();

            principal = userInputDou;

        }

        else

        {

            System.out.println("Please enter an integer for the principal.");

        }

        interest = (principal * rate * time);

        System.out.println("Your interest is $" + interest);

        System.out.println("Your total payment is $" + (interest + principal));

    }

    else if (userInputStr.equalsIgnoreCase("compound interest"))

    {

        System.out.println("Please enter the rate: ");

        if (myScanner.hasNextDouble())

        {

            userInputDou = myScanner.nextDouble();

            rate = userInputDou;

        }

        else

        {

            System.out.println("Please enter a integer for the rate.");

        }

        System.out.println("Please enter the time: ");

        if (myScanner.hasNextDouble())

        {

            userInputDou = myScanner.nextDouble();

            time = userInputDou;

        }

        else

        {

            System.out.println("Please enter an integer for the time.");

        }

        System.out.println("Please enter the principal: ");

        if (myScanner.hasNextDouble())

        {

            userInputDou = myScanner.nextDouble();

            principal = userInputDou;

        }

        else

        {

            System.out.println("Please enter an integer for the principal.");

        }

        System.out.println("Would you like to compound the interest quarterly or annually?");

        userInputStr = myScanner.next();

        if (userInputStr.equalsIgnoreCase("quarterly"))

        {

            System.out.println("For how many quarters would you like to compound the interest for?");

            if (myScanner.hasNextDouble())

            {

                userInputDou = myScanner.nextDouble();

                cNum = (userInputDou * 4);

                subinterest = (principal * (1 + (rate / cNum)));

                interest = Math.pow(subinterest, time);

                total = (principal + interest);

                System.out.println("Your quarterly compounded interest is $" + interest);

                System.out.println("Your total payment is $" + total);

            }

            else

            {

                System.out.println("Please enter an integer for the number of quarters.");

            }

        }

        else if (userInputStr.equalsIgnoreCase("annually"))

        {

            System.out.println("For how many years would you like to compound the interest for?");

            if (myScanner.hasNextDouble())

            {
                userInputDou = myScanner.nextDouble();

                cNum = userInputDou;

                subinterest = (principal * (1 + (rate / cNum)));

                interest = Math.pow(subinterest, time);

                total = (principal + interest);

                System.out.println("Your annually compounded interest is $" + interest);

                System.out.println("Your total payment is $" + total);

            }

            else

            {

                System.out.println("Please enter an integer fro the number of years.");

            }

        }

        else

        {

            System.out.println("Please enter either 'quarterly' or 'annually'.");

        }

    }

}

}