您好我是Java编程的新手,我正在尝试开发一个宠物程序来读取和计算用户输入的宠物类型

时间:2016-06-14 18:44:33

标签: java loops

我尝试过使用Scanner& amp; Stringtokenizer类,用于计算特定宠物的输入次数,但最终获得无输出。我似乎无法弄清楚我需要用什么方法来计算宠物(狗,猫等等)的数量。所以我可以得到狗,猫等数量的正确输出。

String pet,temp, output, dog = "dog", cat ="cat", other ="other";
    double payment, totalPayment = 0, dogCount = 0, catCount = 0, otherCount = 0, totalDogNum = 0;
    int more = 0;



    while(more == JOptionPane.YES_OPTION)
    {
    pet = JOptionPane.showInputDialog(null,
             "Enter the pet type ", "",JOptionPane.QUESTION_MESSAGE);

    temp = JOptionPane.showInputDialog(null,
            "Enter the payment for the appointment ", "",JOptionPane.QUESTION_MESSAGE);
    payment = Double.parseDouble(temp);

    more = JOptionPane.showConfirmDialog(null, "Would you like to add another pet?", "", JOptionPane.YES_NO_OPTION );

    if (pet == dog )
    {
        dogCount = dogCount + 1;
    }
    else if (pet == cat)
    {
        catCount += 1;
    }
    else
    {
        otherCount += 1;
    }

    totalPayment += payment;
    }



    output =  "Number of Dogs: "+ dogCount
            + "\n" + "Number of Cats: " + catCount
            + "\n" + "Number of Other Pets: " + otherCount

1 个答案:

答案 0 :(得分:0)

您应该使用equals方法比较宠物 if (pet.equals(dog) )

将输出字符串打印到控制台:
System.out.println(output);

或JOptionPane
JOptionPane.showInputDialog(null, output, "", JOptionPane.INFORMATION_MESSAGE);