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);
}
}
当我执行代码时,它应该让我输入我目前要承担的学分数和每学分的费用。在输入之后,总结果应该出现在一个新窗口中,短语为"学分成本为*(答案应该来自等式"学费=学分* costPerCredit;") *。这个数字结果应该是我在第一个对话框multiping中放置的数字,预设$ 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?");
。代码似乎以相同的方式工作,但删除了对话框。
答案 0 :(得分:0)
String input;
int credits = 0;
input = JOptionPane.showInputDialog(null,"Number of Credits?");
return credits;
credits
永远不会改变。此代码始终返回0
。
答案 1 :(得分:0)
无法让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);
}