Results 1 to 20 of 22
- 01-03-2010, 01:30 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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
- 01-03-2010, 02:08 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.)
- 01-03-2010, 02:14 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
-
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.
- 01-03-2010, 08:21 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
My apologies
Sorry,you're right of course - will do better in future.
- 01-10-2010, 01:54 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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; } } }
-
- 01-10-2010, 02:42 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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:
Look at how few semicolons there are. They don't go with the "condition" parts.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..."); }Last edited by pbrockway2; 01-10-2010 at 02:44 AM.
- 01-11-2010, 04:01 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
here is where i am now...
here is compiler messageJava 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; } } }
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!!
- 01-11-2010, 05:37 PM #10
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
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.
- 01-11-2010, 05:49 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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
- 01-11-2010, 05:51 PM #12
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++; } } }
- 01-11-2010, 07:44 PM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 01-16-2010, 11:58 PM #14
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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++; } } }
-
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?
- 01-17-2010, 08:00 PM #16
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
hey i got it working at last!
thanks for your help fubarable, what is best way to run test data on this program?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++; } } }
- 01-17-2010, 10:50 PM #17
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
You can use this code
ThanksJava 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");} } } }
- 01-18-2010, 02:13 AM #18
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
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.
- 01-19-2010, 02:04 PM #19
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
thanks fubarable!!
- 12-02-2011, 03:21 AM #20
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
3 basic program
By jasskulainen in forum New To JavaReplies: 3Last Post: 09-27-2009, 01:39 PM -
Basic high score program
By Implode in forum New To JavaReplies: 5Last Post: 09-03-2009, 05:21 PM -
[SOLVED] cant figure this program out..help
By einstein1234 in forum New To JavaReplies: 26Last Post: 04-23-2009, 04:30 AM -
Java assignment - couple methods don't know how to figure out
By Snowboardmylife in forum New To JavaReplies: 1Last Post: 04-16-2008, 10:52 AM -
Basic Program Please Help!!
By VinceGuad in forum New To JavaReplies: 3Last Post: 02-01-2008, 03:35 PM


1Likes
LinkBack URL
About LinkBacks


Bookmarks