[SOLVED] Assistence is needed with my code. ty
Your program should output a title similar to "Welcome to Ken's Grading Program", using your own name of course.
Your program should prompt the user if they will be entering numbers or letters for grades. See example below.
If the user enters 'numbers':
Get input for the grade number in decimal format. i.e. 74.22645 (enter it with 5 decimal places)
Utilize the Math class and round the number to zero decimal places, then use the int cast method (Page 82) to convert to an integer.(ensure you round it before type casting it)
Utilize a switch statement to determine the corresponding letter grade. Use the grading criteria from this course for the numbers. (ensure you accept all allowable numbers i.e. 70-79 and all in between)
Output the corresponding letter grade along with the initial number entered in decimal format utilizing the printf method and format specifiers. Output the number in only 3 decimal places.
if the user enters 'letters':
Get input for the grade letter in String format.
Utilize a switch statement to determine the corresponding output for the letter entered. (ensure you accept both upper and lower case letters.
Use the grading criteria from this course for the numbers. (you must use charAt( ) so the string will work with your switch)
Output the corresponding numbers values for the letter entered along with the letter initially entered.
The code is not allowing my if statement to work, I need it to evaluate the user input and allow it to execute the correct if statement.
Quote:
import java.util.Scanner;
import java.lang.Math;
public class bm
{
/*
* main
*/
public static void main(String[] args) throws Exception
{
Scanner scanner1,scanner2,scanner3;
String sGradeType,sGrade="",sUGrade;
char cGrade;
double dGrade;
long drGrade;
int iGrade=0;
System.out.println("Type of Entry (Chars(C)/Digits(D)");
scanner1 = new Scanner(System.in);
scanner2 = new Scanner(System.in);
sGradeType = scanner1.next();
sGradeType = "D";
if (sGradeType == "D")
{
System.out.println("Enter Grade number");
scanner2 = new Scanner(System.in);
dGrade = scanner2.nextDouble();
drGrade = Math.round(dGrade);
iGrade = (int)dGrade;
switch (iGrade)
{
case 75 :
sGrade = "A" ;
break ;
case 60 :
sGrade = "B";
break ;
case 50 :
sGrade = "C";
break ;
case 40 :
sGrade = "D";
break ;
default :
System.out.print("illegal Grade !") ;
}
System.out.printf("Grade Value, %8.3f. Grade %s", dGrade, sGrade);
} else if (sGradeType == "C")
{
System.out.println("Enter Grade Letter");
scanner3 = new Scanner(System.in);
sGrade = scanner3.next();
sUGrade = sGrade.toUpperCase();
cGrade = sUGrade.charAt(0);
switch (cGrade)
{
case 'A' :
iGrade = 75 ;
break ;
case 'B' :
iGrade = 60;
break ;
case 'C' :
iGrade = 50;
break ;
case 'D' :
iGrade = 40;
break ;
default :
System.out.print("illegal Grade !") ;
}
System.out.printf("Grade Value, %d, Grade %s", iGrade, sGrade);
}
} // end of method main
Use equals() to compare strings
One mistake that you have is with the comparation in the "if" statement:
Code:
if (sGradeType == "D")
Do not use "==" to compare strings... you have to use the String equals() method to compare strings:
Code:
if (sGradeType.equals("D"))
Here's a link to the String API with all the mehtods:
String (Java Platform SE 6)
Luck,
CJSL