Mtangoo
JF-Expert Member
- Oct 25, 2012
- 6,163
- 5,623
Check comments na uzifanyie kazi
nimejaribu kuweka maoni haya kukusaidia kufikia lengo
Code:
/*
* Some Comments on your source code by CTO
* Grades is the class name it should begin with capital to fit conventions (camel case assumed)
* Put spaces between code blocks like for loops and related code groups
* grade = grade/10; is more readble than grade /= 10; its better in terms of self documentation
*
* ===Try and catch==
* Unaweka simba (code) inayotarajia kurusha 'exceptions' katika try block na baada
* tu ya simba unayotaka inaswe inapotokea tatizo weka catch block!
* i.e
* try{....} catch(Exception e){..do something with e}
*
* ==Commandline argument==
* These are received and added to array in order added to exe i.e
* java yourProgram p1 p2 p3
* will give args array of
* args[0]==>p1
* args[1]==>p2
* args[3]==>p3
* Check example before in first code block of main
* Use them as you want
* See this link for details: http://www.codeproject.com/Tips/419749/Working-with-command-line-arguments-in-Java
*/
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
try{ //in case anything happened in code tell us why!
//parse commandline args
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
//rest of your code
int grade = 0;
for (int i = 0; i < 10; i++) {
grade += validInt("Enter mark " + (i + 1) + "[0-100]: ", 0, 100);
}
grade = grade/10;
System.out.print("You earned a " + grade + " ");
if (grade >= 70 && grade <= 100) {
System.out.println("A");
}
else if (grade >= 60 && grade <= 69) {
System.out.println("B+");
}
else if (grade >= 50 && grade <= 59) {
System.out.println("B");
}
else if (grade >= 40 && grade <= 49) {
System.out.println("C");
}
else if (grade >= 30 && grade <= 39) {
System.out.println("D");
}
else {
System.out.println("E");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());// tells you what went wrong
}
}
public static int validInt(String prompt, int min, int max) {
Integer result = null;
while (true) {
System.out.print(prompt);
Scanner in = new Scanner(System.in);
if (in.hasNextInt()) {
result = in.nextInt();
}
if (result != null && result >= min && result <= max) {
return result;
}
else {
System.out.println("Invalid Number!...enter again");
}
}
}
}
Last edited by a moderator: