Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: need help to figure out basic java program

  1. #1
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default need help to figure out basic java program

    i have to write a java program to input 10 student marks,each mark must be checked to see if it is a pass,merit or fail.I can write a program to check 1 mark as follows
    int mark;
    System.out.println("What mark did you get");
    mark=EasyIn.getInt();

    if(mark <= 40)
    {
    System.out.println("You failed");
    }
    else(mark == 41 <= 59)
    {
    System.out.println("You passed");
    }
    else(mark == 60 <= 100)
    {
    System.out.println("You got a merit");
    }
    else
    {
    System.out.println("Invalid mark please re-enter");
    }


    my question is how do i write the progarm so it will ask for 10 student marks?
    i need to use loops or switch statements

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    if(mark <= 40)
    {
    System.out.println("You failed");
    }
    else(mark == 41 <= 59)
    {
    System.out.println("You passed");
    }

    That's not going to fly.

    When you post code use the code tags - there's a button that will wrap the selected text or you can just put [code] at the start and [/code] at the end.

    If there is a compiler message you can't understand, post that as well.

    For general usage of if statements, see the nuts and bolts section of Sun's Tutorial. The section on relational and conditional operators is also worth reading. (And loops are also covered nearby.)

  3. #3
    dwilliams is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default Try something like this...

    Quote Originally Posted by shane123 View Post
    i have to write a java program to input 10 student marks,each mark must be checked to see if it is a pass,merit or fail.I can write a program to check 1 mark as follows
    Try this -my syntax is not much good but here goes

    int numstudents = 0;
    while (numstudents <= 10) {

    int mark;
    System.out.println("What mark did you get");
    mark=EasyIn.getInt();

    if(mark <= 40)
    {
    System.out.println("You failed");
    }
    else(mark == 41 <= 59)
    {
    System.out.println("You passed");
    }
    else(mark == 60 <= 100)
    {
    System.out.println("You got a merit");
    }
    else
    {
    System.out.println("Invalid mark please re-enter");
    }
    }
    out.println("Thats all " + numstudents);

    my question is how do i write the progarm so it will ask for 10 student marks?
    i need to use loops or switch statements
    Try this for size

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by dwilliams View Post
    Try this for size
    To the original poster, please disregard dwilliams' post as it will only lead you astray. Instead, please look at the link provided in the first reply.

    dwilliams, please don't offer code solutions unless you've tested them and know them to work. Bad help is much worse than no help.

  5. #5
    dwilliams is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Unhappy My apologies

    Sorry,you're right of course - will do better in future.

  6. #6
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    hi hope i used tagged this properly,this is where i am now,i think there is a problem with my comparision operators cant figure it out,any help would be great please




    Java Code:
    class examresults
    {
    public static void main(String[]args)
    {
    
    int studentmark=0;
    int studentCounter=1;
    
    
    while(studentCounter<=10) // process 10 students while using counter controlled loop
    {
            System.out.println("What exam mark did you get?");// prompt user for input
            exammark=easyIn.getInt();
    
            if(studentmark>=0) && (studentmark<=40);
            {
            System.out.println("You failed");
            }
            else(studentmark>=41) && (studentmark<=59);
            {
            System.out.println("You passed");
            }
            else(studentmark>=60) && (studentmark<=100);
            {
            System.out.println("You got a merit");
            }
            else
            {
            System.out.println("Invalid mark please re-enter");
            }
    
            studentCounter=studentCounter + 1;
            }
            }
            }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Thanks for using the code tags.

    If you get compiler messages please post them. It helps everyone to know what you are seeing.

    You are still having big problems with the syntax. A typical construction looks like:

    Java Code:
    if(answer == 666 && answer != 666) {
        System.out.println("That's impossible!");
    } else if((answer == 42) || (answer == '*')) { // extra parentheses are OK
        System.out.println("That's correct");
    } else {
        System.out.println("Keep trying...");
    }
    Look at how few semicolons there are. They don't go with the "condition" parts.
    Last edited by pbrockway2; 01-10-2010 at 02:44 AM.

  9. #9
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    here is where i am now...
    Java Code:
    class examresults
    {
    public static void main(String[]args)
    {
    
    int studentmark=0;
    int studentCounter=1;
    
    
    while(studentCounter<=10) // process 10 students while using counter controlled loop
    {
            System.out.println("What exam mark did you get?");// prompt user for input
            studentmark=EasyIn.getInt();
    
            if((studentmark>=0) || (studentmark<=40))
            {
            System.out.println("You failed");
            }
            else if((studentmark>=41)|| (studentmark<=59))
            {
            System.out.println("You passed");
            }
            else if((studentmark>=60) || (studentmark<=100))
            {
            System.out.println("You got a merit");
            }
            else
            {
            System.out.println("Invalid mark please re-enter");
            }
    
            studentCounter=studentCounter + 1;
            }
            }
            }
    here is compiler message

    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are used
    -classpath <path> Specify where to find user class files and annotation processors
    -cp <path> Specify where to find user class files and annotation processors
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -endorseddirs <dirs> Override location of endorsed standards path
    -proc:{none,only} Control whether annotation processing and/or compilation is done.
    -processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process
    -processorpath <path> Specify where to find annotation processors
    -d <directory> Specify where to place generated class files
    -s <directory> Specify where to place generated source files
    -implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -version Version information
    -help Print a synopsis of standard options
    -Akey[=value] Options to pass to annotation processors
    -X Print a synopsis of nonstandard options
    -J<flag> Pass <flag> directly to the runtime system


    Tool completed with exit code 2

    thanks for any help!!

  10. #10
    nessa203 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Quick check: your class name 'examresults' should have a capital at the beginning (due to convention) and the file should be saved as Examresults.java. On the command line you should be within the directory that your file is in and compile it with the command "javac Examresults.java". Are you doing all of that?

    And your if ... else if... else loops should be checking with && instead of || - at the moment you are checking if the student's mark is greater than zero OR less than 40 and then if it's greater than 41 OR less than 59, etc. An input like 5 will cause the program to print "You failed. You passed. You got a merit!"
    Last edited by nessa203; 01-11-2010 at 05:40 PM.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    This has nothing to do with the Java code itself, you were trying to use javac, the java compiler, in a way it didn't understand and it is trying to explain the correct use to you.

    kind regards,

    Jos

  12. #12
    o0oNorao0o's Avatar
    o0oNorao0o is offline Member
    Join Date
    Oct 2009
    Posts
    51
    Rep Power
    0

    Default

    I understood from you .. you want like this code
    I've added read statement (to read the marks from the user)
    Java Code:
    import java.util.Scanner;
    
    public class result
    {
    public static void main(String[]args)
    {
    Scanner EasyIn = new Scanner (System.in);
    int studentmark=0;
    int studentCounter=1;
    System.out.println("Type 10 exam marks :");
    
    while(studentCounter<=10)
    {
           
            studentmark=EasyIn.nextInt();
    
            if((studentmark>=0) || (studentmark<=40))
            {
            System.out.println("You failed");
            }
            else if((studentmark>=41)|| (studentmark<=59))
            {
            System.out.println("You passed");
            }
            else if((studentmark>=60) || (studentmark<=100))
            {
            System.out.println("You got a merit");
            }
            else
            {
            System.out.println("Invalid mark please re-enter");
            }
    
            studentCounter++;
            }
            }
            }

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    OP: if you're still having problems with this, post the command that you used when you got that error message since it was that command that had an error in it.

  14. #14
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    hey i am just finished but when i run the program it is only outputting the first letter of the students name,also is it possible for the output to print capital letters,any help is much appreciated

    Java Code:
    
    class classresult
    {
    public static void main(String[]args)
    {
    int studentmark=0;
    int studentCounter=1;
    char = 0 ;
    
    while(studentCounter<=10)
    {
    
            System.out.println("PLEASE ENTER STUDENTS NAME");
            System.out.println("*****************************");
            studentname=EasyIn.getChar();
            System.out.println("PLEASE ENTER YOUR EXAM MARK :");
            System.out.println("*****************************");
            System.out.println("");
            studentmark=EasyIn.getInt();
    
    
            if((studentmark>=0) && (studentmark<=40))
            {
    
    		System.out.println("");
            System.out.println("SORRY" +studentname+ "BUT YOU FAILED");
            System.out.println("");
            }
            else if((studentmark>=41) && (studentmark<=59))
            {
    	    
    		System.out.println("");
            System.out.println("WELL DONE" +studentname+ "YOU PASSED !");
            System.out.println("");
            }
            else if((studentmark>=60) && (studentmark<=100))
            {
    		
    		System.out.println("");
            System.out.println("CONGRATULATIONS " +studentname+ "YOU GOT A MERIT !!");
            System.out.println("");
            }
            else
            {
    		System.out.println("");
            System.out.println("INVALD MARK PLEASE RE-ENTER");
            System.out.println("");
            studentCounter = studentCounter - 1;
            }
    
            studentCounter++;
    
            }
            }
            }

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your code doesn't compile, or if it does, it's not the code that you've pasted here. Also, are you storing the student name as a String or as a single char? If just a single char, is it possible in any way to magically make a full string out of this?

  16. #16
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    hey i got it working at last!

    Java Code:
    
    class classresult
    {
    public static void main(String[]args)
    {
    int studentmark=0;
    int studentCounter=1;
    String studentname;
    
    while(studentCounter<=10)
    {
    
            System.out.println("PLEASE ENTER STUDENTS NAME");
            System.out.println("*****************************");
            System.out.println("");
            studentname=EasyIn.getString();
            System.out.println("");
            System.out.println("PLEASE ENTER YOUR EXAM MARK :");
            System.out.println("*****************************");
            System.out.println("");
            studentmark=EasyIn.getInt();
            System.out.println("");
    
            if((studentmark>=0) && (studentmark<=40))
            {
    
    		System.out.println("");
            System.out.println("You got: " +studentmark+ "% sorry " +studentname+ " but you failed");
            System.out.println("");
            }
            else if((studentmark>=41) && (studentmark<=59))
            {
    
    		System.out.println("");
            System.out.println("You got: " +studentmark+ "% well done " +studentname+ " you passed !");
            System.out.println("");
            }
            else if((studentmark>=60) && (studentmark<=100))
            {
    
    		System.out.println("");
            System.out.println("You got: " +studentmark+ "% congratulations " +studentname+ " you got a merit !!");
            System.out.println("");
            }
            else
            {
    		System.out.println("");
            System.out.println("INVALD MARK PLEASE RE-ENTER");
            System.out.println("");
            studentCounter = studentCounter - 1;
            }
    
            studentCounter++;
    
            }
            }
            }
    thanks for your help fubarable, what is best way to run test data on this program?

  17. #17
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    You can use this code

    Java Code:
    import java.util.Scanner;
    public class Students {
    	public static void main(String[] args){
    		
    		int mark;
    			
    		Scanner n = new Scanner(System.in);
    		
    		for (int count=1; count<=10; count++){
    		System.out.println("Enter the mark ");
    		mark = n.nextInt();
    		
    		if (mark >=90)
    		{System.out.println("Your credit is A");}
    		else if (mark >=80)
    		{System.out.println("Your credit is B");}
    		else if (mark >=70)
    		{System.out.println("Your credit is C");}
    		else if (mark >=60)
    		{System.out.println("Your credit is D");}
    		else
    		{System.out.println("Sorry, you failed");}
    		}
    	}
    }
    Thanks

  18. #18
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    Shane you had asked if there was a way to print the output in uppercase. You can use the String.toUpperCase() method. There is also a lower case method if you wanted to make them all lower case. Here is an example:

    Java Code:
    System.out.println("hello".toUpperCase());
    Last edited by Fubarable; 01-18-2010 at 02:54 AM.

  19. #19
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    thanks fubarable!!

  20. #20
    Fred999 is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Exclamation reply

    this program wont work easilyand anyway u dont need the else statements after the 1st if-else combination.for 100 students use loop [code], & /code at beginning and ending.

Page 1 of 2 12 LastLast

Similar Threads

  1. 3 basic program
    By jasskulainen in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 01:39 PM
  2. Basic high score program
    By Implode in forum New To Java
    Replies: 5
    Last Post: 09-03-2009, 05:21 PM
  3. [SOLVED] cant figure this program out..help
    By einstein1234 in forum New To Java
    Replies: 26
    Last Post: 04-23-2009, 04:30 AM
  4. Java assignment - couple methods don't know how to figure out
    By Snowboardmylife in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:52 AM
  5. Basic Program Please Help!!
    By VinceGuad in forum New To Java
    Replies: 3
    Last Post: 02-01-2008, 03:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •