Will someone critique my code?
I know this is not very good. I am currently in my first programming
class(java). It seems like it works, but I am sure there are things I have
not thought of. The goal is to have a user enter one by one, how many
credit units each class they need to take is worth. Then enter how many
credit units you will complete in a term. The program should calculate how
many credit units are left, how many terms it will take to complete the
degree, and how much it will cost. Thanks and PLEASE BE GENTLE! import
java.util.ArrayList; import java.util.Scanner;
public class GraduationPlanner {
public static void main(String[] args) {
int sum = 0;
String creditUnits;
int unitsLeft;
int plannedUnits = 0;
int termsLeft = 0;
double tuitionCost = 0;
final double TUITION_RATE = 2890;
ArrayList<Integer> cu = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
System.out.println("Please input the Credit Units for each course.
Press enter after each course. Press Q when finished: ");
while (in.hasNextInt()) {
int temp = (in.nextInt());
if (temp < 0) {
System.out.print("ERROR! Please enter a number 1-9.: ");
} else {
cu.add(temp);
}
}
System.out.println(cu);
Scanner ex = new Scanner(System.in);
System.out.print("Please enter the number of Credit Units you will
complete per term: ");
if (ex.hasNextInt()) {
plannedUnits = ex.nextInt();
}
for (int a : cu)
{
sum += a;
}
unitsLeft = (sum - plannedUnits);
termsLeft = (sum / plannedUnits);
tuitionCost = (termsLeft * TUITION_RATE);
System.out.println("After this term there are " + unitsLeft + " credit
units remaining.");
System.out.println("You will complete your degree in " + termsLeft + "
terms.");
System.out.print("It will cost you " + tuitionCost + " to complete
your degree.");
}
}
No comments:
Post a Comment