我的方法不是随身携带数据(初学者)

时间:2014-10-02 17:57:33

标签: java

这是我的第一个CS项目。在创建我的方法之后,我运行代码以查看它是否工作到目前为止,一切正常但它实际上并没有在方法中进行数学运算。一直在努力,无法找到错误。任何帮助都会很棒。明天到期大声笑。

public static void main(String[] args) {

  int numberOfStudents = 0;
  int total = 0;
  int value = 0;
  int creditHours;
  double tuition;
  int classesMissed;

  System.out.println("Tuition Wasted Based on Student Absences and its effect on GPA.");
  Scanner keyboard = new Scanner(System.in);

  System.out.print("Enter the number of students to consider: ");
  value = keyboard.nextInt();
  while (value >= 5)
  {
    if (value > 5)
      System.out.println("Number of students must be between 1 and 5");
    System.out.print("Please re-enter a value number of students to consider: ");
    value = keyboard.nextInt();
  }

  System.out.print("Enter the student ID for student 1: ");
  value = keyboard.nextInt();

  System.out.print("For how many credit hours is the student registered: ");
  creditHours = keyboard.nextInt();

  System.out.print("Enter the amount of the tuition for the semester: ");
  tuition = keyboard.nextDouble();

  System.out.print("Enter the average number of classes the student misses in a week: ");
  classesMissed = keyboard.nextInt();
  while (classesMissed > creditHours)
  {
    if (classesMissed > creditHours)
      System.out.print("That is not possible, please re-enter the number of classes missed in a week: ");   
    classesMissed = keyboard.nextInt();
  }

  DetermineWastedTuition(creditHours, tuition, classesMissed);

}

public static void DetermineWastedTuition(int creditHours, double tuition, int classesMissed){

  double weeklyTuition;
  weeklyTuition = tuition / 10;
  double weeklyTuitionWasted;
  weeklyTuitionWasted = weeklyTuition *(classesMissed / creditHours);
  double semesterWasted;
  semesterWasted = weeklyTuitionWasted * 16;

  System.out.println("Tuition money wasted each week is " + weeklyTuitionWasted);
  System.out.println("Tuition money wasted each semester is " + semesterWasted);

}

将样本输出为:

根据学生缺勤及其对GPA的影响而浪费学费 输入要考虑的学生人数:1
输入学生1:1234555的学生证 学生注册了多少学分:15
输入学期学费:7500
输入学生在一周内错过的平均课程数量:2
每周浪费的学费是0.0 每个学期浪费的学费是0.0

1 个答案:

答案 0 :(得分:2)

以下计算:

weeklyTuitionWasted = weeklyTuition * (classesMissed / creditHours);
如果0.0

将返回classesMissed < creditHours,因为您要划分两个int变量,因此结果将为int。

将其更改为:

weeklyTuitionWasted = weeklyTuition * ((double) classesMissed / creditHours);