2014-10-02 48 views
-1

这是我的第一个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
输入学生ID为学生1:1234555
对于贷多少小时是学生注册:15
输入学费的金额为学期:7500
输入类的学生错过了一周的平均次数:2,每星期浪费
学费的钱是0.0浪费每学期
学费的钱是0.0

+0

您是否尝试过通过与调试代码加强? – rrirower 2014-10-02 18:00:27

+0

您应该仔细研究代码的缩进,因为它会影响可读性并可能隐藏或隐藏错误。在你的方法开始时真的不需要声明所有的变量。 – 2014-10-02 18:04:33

回答

2

以下计算:

weeklyTuitionWasted = weeklyTuition * (classesMissed/creditHours); 

会返回0.0如果classesMissed < creditHours,因为您将除以两个int变量,因此结果将是一个int。

将其更改为:

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

啊。所以你不能在Java中分割2个整数?我还没有被教过。 – Kesto 2014-10-02 18:44:58

+0

@Kesto当然你可以,但结果是一个整数,所以3/4会返回0,4/3会返回1,等等... – Eran 2014-10-02 18:46:31