Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: using while to solve this question !

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Daring
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default using while to solve this question !

    Write a program that reads the total number of students, and each students name and score, and finally displays the name of the student with the lowest score and the average score.
    Sample run:
    Enter the number of students: 3
    Enter student 1 name: John
    Enter student 1 score: 71
    Enter student 2 name: Chris
    Enter student 2 score: 85.5
    Enter student 3 name: Brad
    Enter student 3 score: 94
    Brad has the lowest score (71) and the average score is 83.5

    i used if statement to solve this question
    PHP Code:
    import java.util.*;
    public class 
    HomeWork4 {
        public static 
    void main(String[] args) {
        
    String student1,student2,student3;
        
    double score1,score2,score3,average=0;
        
    Scanner input=new Scanner(System.in);
        
    System.out.println("Enter student 1 name: ");
        
    student1=input.next();
        
    System.out.println("Enter student 1 score: ");
        
    score1=input.nextDouble();
        
    System.out.println("Enter student 2 name: ");
        
    student2=input.next();
        
    System.out.println("Enter student 2 score: ");
        
    score2=input.nextDouble();
        
    System.out.println("Enter student 3 name: ");
        
    student3=input.next();
        
    System.out.println("Enter student 3 score: ");
        
    score3=input.nextDouble();
        
    average=(score1+score2+score3)/3.0;
        if (
    score2>score3 && score3>score1 || score3>score2 && score2>score1)
           
    System.out.println(student1+" has the lowerst score ("+score1+") and the average score is "+average);    
        else if (
    score3>score1 && score1>score2 || score1>score3 && score3>score2)
           
    System.out.println(student2+" has the lowerst score ("+score2+") and the average score is "+average);
        else if (
    score1>score2 && score2>score3 || score2>score1 && score1>score3)
           
    System.out.println(student3+" has the lowerst score ("+score3+") and the average score is "+average);
        
    System.exit(0); 
    but my teacher wants me to solve it using while method
    PHP Code:
        String name;
        
    double numbers=0,score,studentsnum,average=0;
        
    Scanner input=new Scanner(System.in);
        
    System.out.println("Enter the numbers of students: ");
        
    studentsnum=input.nextDouble();
        while (
    numbers<=studentsnum)
        
    System.out.println("Enter student "+(numbers++)+"name: ");
        
    name=input.next();
        
    System.out.println("Enter student "+(numbers++)+"score: ");
        
    score=input.nextDouble(); 
    i've tried my best but couldn't find a way to complete this thing
    help is appreciated


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: using while to solve this question !

    Suggested reading:
    http://www.javaprogrammingforums.com...-get-help.html
    http://www.javaprogrammingforums.com...e-posting.html
    And private messaging me or anyone else does not increase your chances of receiving help, in fact, quite the opposite.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: using while to solve this question !

    Do you know how to use arrays?
    Comparing the values of many variables gets very messy using if statements.

Similar Threads

  1. Solve Them Please
    By omath in forum Java Theory & Questions
    Replies: 1
    Last Post: December 25th, 2010, 03:26 PM
  2. solve it plz
    By tillu in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2010, 12:45 PM
  3. please solve this question on html:multibox?
    By tejz in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 13th, 2010, 02:18 AM
  4. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 07:28 PM
  5. Replies: 2
    Last Post: May 16th, 2009, 05:23 AM