0

I am trying to do a simple program that validates and allocates grades to a student based on the scores input using both a while loop and a do-while loop. I understand this is simple, the code works fine without validating, but the loops don't loop and program is terminated. This is what I've got so far

import io.*;
public class Marks {
    public static void main(String[] args) {
        double marks, inMarks;
        int assess, inAssess, calcMark, ten;
        boolean dna, dnc, fail, pass, calcDna, calcDnc, calcF, calcP;
        marks = inMarks();
        assess = inAssess();
        dna = calcDna(assess);
        dnc = calcDnc(assess);
        fail = calcF(assess, marks);
        pass = calcP(assess, marks);
        ten = calcMark(marks);

        if (dna == false) {
            if (dnc == true) {
                System.out.println("DNC-" + (int) marks);
            } else if (fail == true) {
                System.out.println("F-" + (int) marks);
            } else if (pass == true) {
                System.out.println(ten + "-" + (int) marks);
            }
        } else {
            System.out.println("DNA");
        }
    }
    private static double inMarks() {
        double marks;
        boolean validMark;
        marks = ConsoleInput.readDouble("Input student's mark");
        while ((marks < 0) && (marks > 100)) {
            System.out.println("invalid mark, please enter again");
            marks = ConsoleInput.readDouble("Input student's mark");
        }
        // Assertion 0 > marks > 100
        return marks;
    }

    private static int inAssess() {
        int assess;
        boolean validAssess;
        do {
            assess = ConsoleInput
                    .readInt("Input number of assessments student completed");
            if ((0 > assess) && (assess > 5)) {
                System.out
                        .println("invalid assessment attendence. please enter again");
            }
        } while ((0 >= assess) && (assess >= 5));
        // Assertion 0 < assess < 5
        return assess;
    }
    private static boolean calcDna(int assess) {
        boolean calcDna;
        calcDna = false;
        if ((assess == 0)) {
            calcDna = true;
        }
        return calcDna;
    }

    private static boolean calcDnc(int assess) {
        boolean calcDnc;
        calcDnc = false;
        if ((assess > 0) && (assess <= 4)) {
            calcDnc = true;
        }
        return calcDnc;
    }

    private static boolean calcF(int assess, double marks) {
        boolean calcF;
        calcF = false;
        if ((assess == 5) && (marks < 50.0)) {
            calcF = true;
        }
        return calcF;
    }

    private static boolean calcP(int assess, double marks) {
        boolean calcP;
        calcP = false;
        if ((assess == 5) && (marks > 50.0)) {
            calcP = true;
        }
        return calcP;
    }

    private static int calcMark(double marks) {
        int ten;
        ten = (int) marks / 10;
        return ten;

    }
}
5
  • 5
    I don't see any loop there. Commented Sep 30, 2013 at 17:31
  • Don't you think fail == true is a little redundant? Commented Sep 30, 2013 at 17:32
  • Which loops are you referring to? Can you highlight the ones not working in the code. Commented Sep 30, 2013 at 17:37
  • 1
    @Rohit Jain the loops are in the methods inMarks and inAssess. Commented Sep 30, 2013 at 17:40
  • i'm referring to both loops in inMarks and inAssess Commented Sep 30, 2013 at 17:40

3 Answers 3

2

First problem:

while( (marks < 0) && (marks > 100))

Must be or:

while( (marks < 0) || (marks > 100))

No number exists is less than 0 and larger than 100 :)

do {
    assess = ConsoleInput.readInt ("Input number of assessments student completed");
    if((0 <= assess ) || (assess >= 5))
    {
        System.out.println("invalid assessment attendence. please enter again");
    }
} while ((0 <= assess) || ( assess >= 5));
Sign up to request clarification or add additional context in comments.

3 Comments

this solves the problem with the inMarks but if i did the same with inAssess it infinitely loops if assess input is 5 or 0
i've got not enough rep sorry :S
@hasan Ryane's edit was correct, but it was rejected as a too radical change of your original post. Please fix the code yourself - you've inverted the conditions. It is a good habit to test your code before you post it as a solution.
2

If by "the loops dont loop", you're referring to this:

while( (marks < 0) && (marks > 100))

then your problem is that marks cannot possibly be both less than zero and greater than 100.

Similar problem here:

   } while ((0 >= assess) && ( assess >= 5));

assess cannot be both less than zero and greater/equal to 5.

Comments

1

changed

while( (marks < 0) && (marks > 100));

while ((0 >= assess) && ( assess >= 5));

to

while( (marks < 0) || (marks > 100))

while ((0 > assess) && ( assess > 5));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.