Hi I am just trying to figure the if-else statements and I was just wondering why on the lines with
if (Grade == "A" || Grade== "B" || Grade=="C" || Grade== "D" || Grade=="F")
{
System.out.println ("Enter Percentage Weights: ");
}
else
{ System.exit(0);
}
System.out.println ("Exam 1: ");,
that even if I put A it just exits and also on the lines
EX1Score = keyboard.next();
if (EX1Score == "y" || EX1Score == "yes" || EX1Score == "Y" || EX1Score == "YES")
{
System.out.println("Marks scored in Exam 1:");
int Exam1Score = keyboard.nextInt();//The numerical value of the user’s score on exam one
}
else
{
System.out.println("Do you know your lab score");
why even if I put y or the others it skips to the lab question anyway
import java.util.Scanner;
public class GradeCalculation {
/*
* GradeCalculation.java
* Author: Nawasca Baity
* Last edited: 02/15/2010
*
* Purpose: A brief one or two paragraph description of the
* program. What does it do? How does it do it?)
*
* Statement of Academic Honesty:
*
* The following code represents my own work. I have neither
* received nor given inappropriate assistance. I have not copied
* or modified code from any source other than the course webpage
* or the course textbook. I recognize that any unauthorized
* assistance or plagiarism will be handled in accordance with
* the University of Georgia’s Academic Honesty Policy and the
* policies of this course.
*
*/
public static void main(String[] args) {
String Grade;// This will be the overall grade the user hopes to make in the class
String EX1Score;//Yes or No if they know the numerical grade of exam one
String EX2Score;//Yes or No if they know the numerical grade of exam two
String FinalExamScore;//Yes or No if they know the numerical grade of final exam
String LabGrade; //Yes or No if they know the numerical grade of Labs
String ProjectGrade; //Yes or No if they know the numerical grade of projects
System.out.println ("Grading Scale");
System.out.println ("A 90 - 100");
System.out.println ("B 80 - 89");
System.out.println ("C 70 - 79");
System.out.println ("D 50 - 69");
System.out.println ("F Below" );
Scanner keyboard= new Scanner (System.in);
System.out.println ("What letter grade do you want to achieve for the course?: ");
Grade = keyboard.next();
if (Grade == "A" || Grade== "B" || Grade=="C" || Grade== "D" || Grade=="F")
{
System.out.println ("Enter Percentage Weights: ");
}
else
{ System.exit(0);
}
System.out.println ("Exam 1: ");
int Exam1 = keyboard.nextInt();//The weight of exam one
System.out.println ("Exam 2: ");
int Exam2 = keyboard.nextInt();//The weight of exam two
System.out.println ("Final Exam: ");
int FinalExam = keyboard.nextInt();//The weight of the final exam
System.out.println ("Labs: ");
int Labs = keyboard.nextInt();//The weight of the labs
System.out.println ("Projects: ");
int Projects = keyboard.nextInt();//The weight of the projects
if (Exam1 + Exam2 + FinalExam + Labs + Projects <100)
{System.out.println("Weights don’t add up to 100, program exiting… ");
System.exit(0);//System exits if the values of the weight do not add up to 100
}
else
{ System.exit(0);
}
System.out.println ("Enter your scores out of a 100: ");
System.out.println ("Do you know your Exam 1 score?");
EX1Score = keyboard.next();
if (EX1Score == "y" || EX1Score == "yes" || EX1Score == "Y" || EX1Score == "YES")
{
System.out.println("Marks scored in Exam 1:");
int Exam1Score = keyboard.nextInt();//The numerical value of the user’s score on exam one
}
else
{
System.out.println("Do you know your lab score");
LabGrade = keyboard.next();
System.out.println("Do you know your project score");
ProjectGrade = keyboard.next();
}
}
}
Thank You, I just can’t seem to figure it out
If you use the == operator with strings, it only returns true if they’re the EXACT SAME object. Example:
String x="A";
String y="A";
x==x; //true
x==y;//false
x=="A";//false
What you want is if the string contents are the same. Use the String.equals(String) method instead.
x.equals(y);//true