Results 1 to 6 of 6
- 11-21-2012, 10:02 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Review my code please - simple grade calculator program, need it perfect
Ok so ive being doing java in college for about two months and i have been given an assignment ! Don't worry im not asking any one to do my home work for me :P i have the task completed and running, but i want some one more more knowledge than me to look at my code and be sure its ok, Its very important that i get 100% in this assignment so i don't want to submit it if the code isnt up to scratch.
My task is "Write a programme to read in 10 students marks from the keyboard. as each mark is input it must be checked to see wheather it is a pass, fail or merit." each mark is out of a possible 100 (0-40 fail,41-59 pass,60-100merit)
add extra checks for marks less than 1 or greater than 100 and output an invalid data message. program should continue until 10 valid marks have been entered and graded, check thoroughly so that it works under all conditions"
"you are required to use some or all of the following
conditional statements
loop constructs
array constructs
string methods
case statements"
and my code is
One thing im not sure how to do is check to see if the user entered a character instead of a number and output that same invalid data message without incrementing loopJava Code:package assignment1; import java.util.Scanner; public class Assignment1 { public static void main(String[] args) { Scanner imput = new Scanner(System.in); //Creating new Scannr obj System.out.print("****Student Grade Calculator*****\n"); System.out.println("*********************************\n"); //layout int [] grades = new int [arySize]; //declaring & initializing array of type int //this will hold the grades for(int i =0;i<grades.length;i++){ //loop to cycle through array System.out.print("Enter Grade of student number "+(i+1)+" :"); grades[i]=imput.nextInt(); //Getting user imput of grade if (grades[i]>=1 && grades[i]<=40){ System.out.println("Student mark is " + grades[i]+", Grade is Fail\n"); } //if Statement when user enters mark greater than or less than one // and less than or equal to 40 print to screen else if (grades[i]>=41 && grades[i]<=59){ System.out.println("Student mark is " + grades[i]+", Grade is Pass\n"); } else if (grades[i]>=60 && grades[i]<=100){ System.out.println("Student mark is " + grades[i]+", Grade is Merit\n"); } else if (grades[i]<0 || grades[i]>100){ System.out.println("INVALAD MARK - PLEASE RE-INPUT DATA\n"); i--; } }//end for loop } }
thanks in advance for any input and sorry if i posted the code in wrong format not sure how to get it on page looking like everyone else's i use netbeans!Last edited by ronanbrowne88; 11-21-2012 at 10:13 PM.
- 11-22-2012, 12:18 AM #2
Re: Review my code please - simple grade calculator program, need it perfect
The Scanner class has a hasNextInt method.
- 11-22-2012, 12:31 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
- 11-22-2012, 12:36 AM #4
Re: Review my code please - simple grade calculator program, need it perfect
So I mention a method you do not know about. What should be the first thing you do? Go read about it in the Java API. Write some test code to see how it works. When you are confident, add it to your code. Then if you are still stuck come back here, post your code attempt, post full error messages and ask a question.
- 11-22-2012, 11:54 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
Re: Review my code please - simple grade calculator program, need it perfect
You may have solved this already and not posted, but nextInt() returns the next integer that it receives from System.in, or User input in laymen terms. Whereas hasNextInt() returns whether or not(Boolean, which is a variable that is either true, or false.) there is an integer inputted. So you should safely be able to use it like this:
Java Code:if(SystemInput.hasNextInt()) { //Put all of the stuff you want to do If the //Input is an integer }else if(SystemInput.hasNextChar()) { //Put all of what you want to do if the input //is a character. }
- 11-23-2012, 02:00 AM #6
Re: Review my code please - simple grade calculator program, need it perfect
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Problem with grade calculator?
By MysteryForumGuy in forum New To JavaReplies: 3Last Post: 02-15-2012, 01:14 AM -
Having trouble with simple grade averages/raised averages program
By LogicalOutlier in forum New To JavaReplies: 7Last Post: 01-20-2012, 09:44 PM -
Perfect number 1-1000 program help
By ImWithStupid in forum New To JavaReplies: 9Last Post: 04-28-2011, 08:22 AM -
Peculiarty in code of simple program...
By Kreuz14 in forum New To JavaReplies: 4Last Post: 01-23-2008, 03:27 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks