Help bolean method and while loop !!!!
Good afternoon, everyone I have a problem with my program. My last method (isValid) checks if the input is valid from 1 to 100 when I call the method with the while loop in the first input it works fine. but in the second input it wont work you can try to enter 101 and it would continue to the 3rd input I dont know whats wrong! in the input #2 i changed the while loop to compare it to score2 which is input 2 but nothing it wont check if its a valid input
PHP Code:
import java.util.Scanner;
public class TestAverageAndLetterGrade
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int score1;
int score2;
int score3;
int score4;
int score5;
System.out.println("Enter your 1st test score: ");
score1 = keyboard.nextInt();
boolean isValid = isValid(score1);
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score1 = keyboard.nextInt();
isValid = isValid(score1);
}
System.out.println("Enter your 2nd test score: ");
score2 = keyboard.nextInt();
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score2 = keyboard.nextInt();
isValid = isValid(score2);
}
System.out.println("Enter your 3rd test score: ");
score3 = keyboard.nextInt();
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score3 = keyboard.nextInt();
isValid = isValid(score3);
}
System.out.println("Enter your 4th test score: ");
score4 = keyboard.nextInt();
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score4 = keyboard.nextInt();
isValid = isValid(score4);
}
System.out.println("Enter your 5th test score: ");
score5 = keyboard.nextInt();
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score5 = keyboard.nextInt();
isValid = isValid(score5);
}
System.out.println("=========================================");
double average = calculateAverage(score1, score2, score3, score4, score5);
System.out.println (" The average of the 5 test scores is: " + gradeLetter(average));
gradeLetter(average);
System.out.print(" test #1");
gradeLetter(score1);
System.out.print(" test #2");
gradeLetter(score2);
System.out.print(" test #3");
gradeLetter(score3);
System.out.print(" test #4");
gradeLetter(score4);
System.out.print(" test #5");
gradeLetter(score5);
}
public static double calculateAverage(int score1,
int score2,
int score3,
int score4,
int score5)
{
double average = (score1 + score2 + score3 + score4 + score5) / 5;
return average;
}
public static double gradeLetter(double average)
{
if (average>90)
{
System.out.println(" You received an A :) ");
}
else if (average>=80)
{
System.out.println(" You received a B");
}
else if (average>=70)
{
System.out.println(" You received a C ");
}
else if (average>=60)
{
System.out.println(" You received a D");
}
else if (average<60)
{
System.out.println(" You received an F :( ");
}
return average;
}
public static boolean isValid(int score)
{
boolean status;
status = true;
if(score >= 1 && score <= 100);
if(score >= 1 && score <= 100);
// if(score3 >= 1 && score3 <= 100);
// if(score4 >= 1 && score4 <= 100);
// if(score5 >= 1 && score5 <= 100);
else
status = false;
return status;
}
}
Re: Help bolean method and while loop !!!!
here is the code i fixed you need to put isValid(score2) before the second while loop executes and do this for all if you copy and paste this code it should work. I also dont think you need isValid(score2) and so forth inside your while loops.just
...edited by moderator
Re: Help bolean method and while loop !!!!
Is your problem solved then?
Re: Help bolean method and while loop !!!!
his problem is solved then
Re: Help bolean method and while loop !!!!
Quote:
Originally Posted by
NewAtJava
his problem is solved then
I did not realize another user posted that response. NewAtJava, I highly recommend reading the forum rules, as well as the following:
http://www.javaprogrammingforums.com...n-feeding.html
Re: Help bolean method and while loop !!!!
yes i understand but i wish someone would spoon feed me sometimes so i could study there code and learn. To bad know one spoon feeds me!
Re: Help bolean method and while loop !!!!
true spoon feeding is not always helpful but in my case It would have been cause java programming forums is my last choice after i have studied and checked every possible possibility for my java problems so a spoon fed answer is not bad sometimes.
Re: Help bolean method and while loop !!!!
Quote:
Originally Posted by
NewAtJava
true spoon feeding is not always helpful but in my case It would have been cause java programming forums is my last choice after i have studied and checked every possible possibility for my java problems so a spoon fed answer is not bad sometimes.
If you read the link, you would understand why there is a forum policy and why spoonfeeding is frowned upon. It robs you of learning how to problem solve, and if you cannot learn how to problem solve then you're programming skills will never improve. If you wish to discuss this elsewhere that is fine, but let's please not hijack someone else's post.
Re: Help bolean method and while loop !!!!
Quote:
Originally Posted by
NewAtJava
here is the code i fixed you need to put isValid(score2) before the second while loop executes and do this for all if you copy and paste this code it should work. I also dont think you need isValid(score2) and so forth inside your while loops.just
...edited by moderator
Thanks for your help NewAtJava and not thinking your some hot *** ! I tried what you told me but nothing I put isValid(score2) and so on for all the inputs but the method doesn't seem to work only for the firs input..... and as for the isValid in isValid = isValid(score2); in the while loop if i were to take that out wouldn't it be an infinite loop. heres the code re edited idk if that how you meant the method should be called
PHP Code:
import java.util.Scanner;
public class TestAverageAndLetterGrade
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int score1;
int score2;
int score3;
int score4;
int score5;
System.out.println("Enter your 1st test score: ");
score1 = keyboard.nextInt();
boolean isValid = isValid(score1);
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score1 = keyboard.nextInt();
isValid = isValid(score1);
}
System.out.println("Enter your 2nd test score: ");
score2 = keyboard.nextInt();
isValid(score2);
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score2 = keyboard.nextInt();
isValid = isValid(score2);
}
System.out.println("Enter your 3rd test score: ");
score3 = keyboard.nextInt();
isValid(score3);
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score3 = keyboard.nextInt();
isValid = isValid(score3);
}
System.out.println("Enter your 4th test score: ");
score4 = keyboard.nextInt();
isValid(score4);
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score4 = keyboard.nextInt();
isValid = isValid(score4);
}
System.out.println("Enter your 5th test score: ");
score5 = keyboard.nextInt();
isValid(score5);
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score5 = keyboard.nextInt();
isValid = isValid(score5);
}
System.out.println("=========================================");
double average = calculateAverage(score1, score2, score3, score4, score5);
System.out.println (" The average of the 5 test scores is: " + gradeLetter(average));
gradeLetter(average);
System.out.print(" test #1");
gradeLetter(score1);
System.out.print(" test #2");
gradeLetter(score2);
System.out.print(" test #3");
gradeLetter(score3);
System.out.print(" test #4");
gradeLetter(score4);
System.out.print(" test #5");
gradeLetter(score5);
}
public static double calculateAverage(int score1,
int score2,
int score3,
int score4,
int score5)
{
double average = (score1 + score2 + score3 + score4 + score5) / 5;
return average;
}
public static double gradeLetter(double average)
{
if (average>90)
{
System.out.println(" You received an A :) ");
}
else if (average>=80)
{
System.out.println(" You received a B");
}
else if (average>=70)
{
System.out.println(" You received a C ");
}
else if (average>=60)
{
System.out.println(" You received a D");
}
else if (average<60)
{
System.out.println(" You received an F :( ");
}
return average;
}
public static boolean isValid(int score)
{
boolean status;
status = true;
if(score >= 1 && score <= 100);
if(score >= 1 && score <= 100);
// if(score3 >= 1 && score3 <= 100);
// if(score4 >= 1 && score4 <= 100);
// if(score5 >= 1 && score5 <= 100);
else
status = false;
return status;
}
}
Re: Help bolean method and while loop !!!!
Hello pip0!
What do you need the boolean variable isValid for? You can test directly your isValid(int score) method in the while loops since it returns a boolean value. To be more clear:
instead of
Code java:
boolean isValid = isValid(score1);
while (isValid != true )
you can just do this:
Hope it helps.
Re: Help bolean method and while loop !!!!
I ran your program on my computer and it worked fine with copy and paste the one i left on there for you. It worked on mine perfectly every time i put a higher number than 100 it said enter correct number and when i put the right one in it moved on correctly. I tried it again it works perfect!
Re: Help bolean method and while loop !!!!
here it is again hope this helps I know how it can be to figure out a program problem its frustrating sometimes!
...edited by moderator
Re: Help bolean method and while loop !!!!
@NewAtJava, you've been warned, and this time issued an infraction for violating forum rules. I'm a very patient moderator, but your last post completely disregarded my previous warning. One more and you will be banned.
Re: Help bolean method and while loop !!!!
Quote:
Originally Posted by
pip0
Thanks for your help NewAtJava and not thinking your some hot *** ! I tried what you told me but nothing I put isValid(score2) and so on for all the inputs but the method doesn't seem to work only for the firs input..... and as for the isValid in isValid = isValid(score2); in the while loop if i were to take that out wouldn't it be an infinite loop. heres the code re edited idk if that how you meant the method should be called
First, please watch you're language. Second, there is a big difference between calling this
Code :
boolean isValid = isValid(score1);
vs:
The first (used for your first validation) actually uses the returned value in subsequent code, the second (used for the other validations) does not.
Re: Help bolean method and while loop !!!!
Quote:
Originally Posted by
copeg
First, please watch you're language. Second, there is a big difference between calling this
Code :
boolean isValid = isValid(score1);
vs:
The first (used for your first validation) actually uses the returned value in subsequent code, the second (used for the other validations) does not.
Okay " Super Moderator " so can you please explain what I'm doing wrong and have some patient for the people who are trying to learn! not everyone knows it all ! I don't want to be spoon fed I just want to understand and learn.
Re: Help bolean method and while loop !!!!
Quote:
Originally Posted by
pip0
Okay " Super Moderator " so can you please explain what I'm doing wrong and have some patient for the people who are trying to learn! not everyone knows it all ! I don't want to be spoon fed I just want to understand and learn.
I must be missing something...what about enforcing rules, helping run these forums, and helping people in my valuable spare time relates to not having patience, and deserving of a reply like that? You don't pay for this service, and you should consider yourself lucky that forums such as this exist - and exist because of moderators and contributors such as myself who donate their time.
This thread isn't about enforcing rules, its about helping you...and I presume you don't want this thread to side-track into another topic entirely.
What exactly did you not understand about my post above, the one which you quoted? Think about it this way
Code :
boolean isValid = isValid(score1);//suppose this call to isValid returns true
if ( isValid ){
//do something
}
isValid(score2);//suppose this call to isValid returns false
if ( isValid ){//the variable isValid was never set by the call in the above line, so it will have the same value as when it was last set.
}