Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java help

Options
  • 25-02-2016 12:49am
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi I am studying Java and I am finding the for loop a little confusing for example how would I solve this problem, write a for loop to enter the grade of 10 students, then display what percentage got an honour. An honour is over 70... Can ye please explain the answer in parts as I want to study it and make myself a similar question to test myself, thanks in advance!


Comments

  • Moderators, Society & Culture Moderators Posts: 17,642 Mod ✭✭✭✭Graham


    You might want to explain what you've tried so far otherwise your question could be mistaken for a 'please do my homework' thread.


  • Moderators, Politics Moderators Posts: 39,098 Mod ✭✭✭✭Seth Brundle


    Hi I am studying Java and I am finding the for loop a little confusing for example how would I solve this problem, write a for loop to enter the grade of 10 students, then display what percentage got an honour. An honour is over 70... Can ye please explain the answer in parts as I want to study it and make myself a similar question to test myself, thanks in advance!
    I won't be giving you the code which you should have anyhow.
    Set up your for loop iterating ten times.
    During each iteration, it should check the score. If it's over seventy then update a variable e.g. x.
    When the loop has finished, x will tell you how many of the ten got an honour. Convert this to a percentage.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I'm not looking for "do my homework" I want to understand how it's done, I have set up the loop to run 10 times and then I have started a if else but then I get lost


  • Registered Users Posts: 725 ✭✭✭Morpork


    Post what you have and we can explain how it work or what's wrong if anything.


  • Moderators, Politics Moderators Posts: 39,098 Mod ✭✭✭✭Seth Brundle


    Show your code please


  • Advertisement
  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Int student;
    Int mark;

    Double percent;

    For (student=1, student <10, student++){

    String studentStr=JOptionPane.showInputDialog("enter the mark");

    studentStr=Integer.parseInt(student);

    If (mark <=70)


  • Registered Users Posts: 69 ✭✭schaffer1969


    Without doing your homework for you I've give you one possible basic solution

    int maxStudentCount = 10;
    int honourCount = 0;


    for(int studentCount=0;studentCount<maxStudentCount;studentCount++){
    // prompt to accept value from console
    // look at Scanner class

    // check if entered value is >= 70
    // if so add to count - otherwise do nothing

    }

    // calculate percentage - honourCount/maxStudentCount *100

    // output percentage result to console


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Just to clarify this is not homework I have a test on Friday so I want to understand how this works , thank you for you suggestion but we have no learned to put a less than inside or for loops yet


  • Registered Users Posts: 69 ✭✭schaffer1969


    Int student;
    Int mark;

    Double percent;

    For (student=1, student <10, student++){

    String studentStr=JOptionPane.showInputDialog("enter the mark");

    studentStr=Integer.parseInt(student);

    If (mark <=70)


    Try getting the core logic correct before you start coding or using GUI libraries.
    Once pseudocode is done look at simplifying the process.
    Read from the console or hard code in the values first.
    Once that is working replace hard coded input to accept input from GUI.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    As in studentcount<maxstudentcount. I now see you had that = to 10 so is that the same as putting <10 in the for loop? ... How can I get my program to count all over 70 grades would this be done with count++;


  • Advertisement
  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    How can I make my program like this ... If (mark <70)
    Don't count it
    Else
    Count it
    .....
    Is it possible to do this in Java


  • Registered Users Posts: 69 ✭✭schaffer1969


    As in studentcount<maxstudentcount. I now see you had that = to 10 so is that the same as putting <10 in the for loop? ... How can I get my program to count all over 70 grades would this be done with count++;

    For a start your for loop count should start at 0 if you are using <10.
    Otherwise if 1 is used you must use <=10 as you want it to loop 10 times.

    Loop through all 10 grades. For each iteration check if grade >=70. If so increment a count variable.
    Once loop is finished i.e. it has looped 10 times, calculate the percentage of honours grades


  • Registered Users Posts: 69 ✭✭schaffer1969


    How can I make my program like this ... If (mark <70)
    Don't count it
    Else
    Count it
    .....
    Is it possible to do this in Java

    Yes but it's bad programming.
    You are forcing at least 2 checks to be done, the if and the else if, while only one is needed.
    The else would get around this but in this case would also be bad programming practice.

    if(mark <70){
    // do nothing
    } else{
    // add count
    }


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Thank you for your help I think I'll be able to figure it out, I'll try it again tommorrow. Programming can get very confusing


  • Registered Users Posts: 6,236 ✭✭✭Idleater


    Here's a non programming example on how to get your mind around your for loop problem:

    Take a notepad and pencil and go stand at your door/window where you can see the street;
    Count the next 10 cars that go past noting down the colour of each;
    After 10 cars, read through the list of colours and note the amount of white cars;


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Thank you all for the help!


Advertisement