【问题标题】:Can't get JOptionPane and System.out.println to work in tuition calculator无法让 JOptionPane 和 System.out.println 在学费计算器中工作
【发布时间】:2017-10-02 19:32:24
【问题描述】:
import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Tuition
{
    public static void main(String[] args)
    {
        //Declareing the variables
        int credits;
        double costPerCredit;
        double tuition;

        //calling methods
        credits = getCredits();
        costPerCredit = getCostPerCredit(credits);
        tuition = calcTuition(credits, costPerCredit);
        displayCredits(costPerCredit);
        displayTotal(tuition);


        //Dialog box that is popping out to ask the questions and get imput based on questions
        JOptionPane.showMessageDialog(null, "Java Tuition Calculator Program");
    }

    public static int getCredits()
    {
        //declare hours variable
        String input;
        int credits = 0;
        input = JOptionPane.showInputDialog(null,"Number of Credits?");

        return credits;
    }

    public static double getCostPerCredit(int credits)
    {
        String input1;
        double costPerCredit;
        costPerCredit = credits * 107.78;
        input1 = JOptionPane.showInputDialog(null,"Cost Per Credit?");

        return costPerCredit;
    }

    public static double calcTuition(int credits, double costPerCredit)
    {
        double tuition;

        tuition = credits * costPerCredit;
        return tuition;
    }


    public static void displayCredits(double costPerCredit)
    {
        double credits;
        credits = costPerCredit;

        DecimalFormat twoDigits = new DecimalFormat("$#000.00");
        System.out.println("Cost for Credits are " + credits);
    }

    public static void displayTotal(double tuition) 
    {
        double total;
        total = tuition;

        DecimalFormat twoDigits = new DecimalFormat("$#000.00");
        System.out.println("Your Tuition is " + total);
    }
}

当我执行代码时,它应该会要求我输入我目前要获得的学分数量和每学分的成本。在我输入这些之后,总结果应该出现在一个新窗口中,其中包含“学分成本是*(答案应该来自等式“tuition = credits * costPerCredit;”)*。这个数字结果应该是数字我将第一个对话框与预设的 $107.78 相乘,我希望这些答案出现在最后弹出的新窗口中。但我得到的只是

Cost for Credits are 0.0
Your Tuition is 0.0

为什么我不能让 System.out.println 从public static double getCostPerCredit(int credits)public static int getCredits() 打印出答案?为他们俩弹出对话框并询问该字段中的问题,但我希望打印出这些答案,而不仅仅是答案为“0.0”。

注意:我取出了String input1;input1 = JOptionPane.showInputDialog(null,"Cost Per Credit?");。代码似乎在没有它的情况下以相同的方式工作,但对话框被删除了。

【问题讨论】:

标签: java swing joptionpane


【解决方案1】:

无法让 JOptionPane 和 System.out.println 在学费中工作 计算器

First: Cost for Credits are 0.0 because of int credits = 0; and you return credits 0 as type int

Second: Any thing multiply by 0.0 is 0.0 as type double, So Your Tuition is 0.0
public static int getCredits()
    {
        //declare hours variable
        String input;
        int credits = 0;
        input = JOptionPane.showInputDialog(null,"Number of Credits?");

        return Integer.parseInt(input);
    }

【讨论】:

  • 感谢 John Kugelman 和 Soumya Das 的帮助,非常感谢。再次感谢您:D
【解决方案2】:
String input;
int credits = 0;
input = JOptionPane.showInputDialog(null,"Number of Credits?");

return credits;

credits 永远不会改变。此代码始终返回0

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多