question how to make this test app work

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1291 Views - Last Post: 10 September 2011 - 07:45 AM Rate Topic: -----

#1 moparman426  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 32
  • Joined: 08-September 11

question how to make this test app work

Posted 09 September 2011 - 01:30 PM

hello yes if your also do vb and seen me iam also reading and teaching myself alil java its not an easy path but trying most exercises anyways i have this problem its suppose to modify the if statement so it says invalid entry not counted and user score is greater than 100 but is not 999 because when 999 is entered the program ends. iam having trouble getting the if statement message to appear than just whats there here is my code so far:
hopefully someone can assit me please

import java.util.Scanner;

public class Firstclass
{
    public static void main(String[] args)
    {
        // display operational messages
        System.out.println("Please enter test scores that range from 0 to 100.");
        System.out.println("To end the program enter 999.");
        System.out.println();  // print a blank line

        // initialize variables and create a Scanner object
        double scoreTotal = 0;
        int scoreCount = 0;
        int testScore = 0;
        Scanner sc = new Scanner(System.in);

    	// get a series of test scores from the user
        while (testScore < 999)
        {
		    // get the input from the user
            System.out.print("Enter score: ");
        	testScore = sc.nextInt();

        	// accumulate score count and score total
        	if (testScore < 999)
        	{
        		scoreCount = scoreCount + 1;
        		scoreTotal = scoreTotal + testScore;
			}
        	System.out.println()
		}

    	// display the score count, score total, and average score
    	double averageScore = scoreTotal / scoreCount;
    	String message = "\n" +
    	                 "Score count:   " + scoreCount + "\n"
    	               + "Score total:   " + scoreTotal + "\n"
		               + "Average score: " + averageScore + "\n";
		System.out.println(message);
	}
} 

This post has been edited by macosxnerd101: 09 September 2011 - 02:28 PM
Reason for edit:: Please use code tags. Also, please do not post help questions in the tutorials section. Moved to Java help.


Is This A Good Question/Topic? 0
  • +

Replies To: question how to make this test app work

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: question how to make this test app work

Posted 09 September 2011 - 02:36 PM

Just as you have this statement to ensure the score is <999:
  	if (testScore < 999)
      	{
       		scoreCount = scoreCount + 1;
       		scoreTotal = scoreTotal + testScore;
	}

You need a test to ensure the score is > 100 && score is < 999.

How can you modify the above to accomplish that?

This post has been edited by GregBrannon: 09 September 2011 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 1091100582  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 15-August 09

Re: question how to make this test app work

Posted 09 September 2011 - 10:00 PM

hmm,i guess you can give out a restriction that your user have to input the score within the range o to 100

testScore>= 0 && testScore<=100


then only give out an extra condition,whether meet some certain condition ,it will terminate your program and produce your result
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: question how to make this test app work

Posted 10 September 2011 - 12:16 AM

That's a start. Your first post says you want to "modify the if statement." Your current if statement is:

if (testScore < 999)
{
    scoreCount = scoreCount + 1;
    scoreTotal = scoreTotal + testScore;
}

As you said, you want to modify that to:
if ( testScore < 0 || testScore > 100 )  // invalid scores
{
    System.out.println( "Invalid entry" );
}
else
{
    scoreCount = scoreCount + 1;
    scoreTotal = scoreTotal + testScore;
}


Edit: Except you want it to be invalid in EITHER case, so the two conditions should be ORd, not ANDed. I corrected above.

This post has been edited by GregBrannon: 10 September 2011 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 moparman426  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 32
  • Joined: 08-September 11

Re: question how to make this test app work

Posted 10 September 2011 - 05:57 AM

well idk if you corrected or not i tried your code piece and idea and it didnt work iam still able to enter a score greater than 100 but isnt 999 i enter 250 and it goes back to enter score it is suppose to give me the error message but isnt iam soo stumped please help me this is what it says after i type in say 25000:

Enter score: 25000

Score count: 0
Score total: 0.0
Average score: NaN

and thats with your code spitet so any thoughts as to why the error message isnt appearing ???
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: question how to make this test app work

Posted 10 September 2011 - 05:59 AM

You'll have to show your latest code to get specific help.
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: question how to make this test app work

Posted 10 September 2011 - 06:00 AM

Can you please repost your new code that you have so that we can see what we have to work with now?

Edit - Ninja'd by Greg, oh these early mornings.. with Code Tags please

This post has been edited by Fuzzyness: 10 September 2011 - 06:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 moparman426  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 32
  • Joined: 08-September 11

Re: question how to make this test app work

Posted 10 September 2011 - 06:01 AM

alright here is the code modified so far:

[import java.util.Scanner;

public class TestScoreApp
{
public static void main(String[] args)
{
// display operational messages
System.out.println("Please enter test scores that range from 0 to 100.");
System.out.println("To end the program enter 999.");
System.out.println(); // print a blank line

// initialize variables and create a Scanner object
double scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
Scanner sc = new Scanner(System.in);

// get a series of test scores from the user
while (testScore > 100)
{
// get the input from the user
System.out.print("Enter score: ");
testScore = sc.nextInt();

// accumulate score count and score total
if ( testScore < 0 || testScore > 100 ) // invalid scores
{
System.out.println( "Invalid entry" );
}


{
scoreCount = scoreCount + 1;
scoreTotal = scoreTotal + testScore;
}
}

// display the score count, score total, and average score
double averageScore = scoreTotal / scoreCount;
String message = "\n" +
"Score count: " + scoreCount + "\n"
+ "Score total: " + scoreTotal + "\n"
+ "Average score: " + averageScore + "\n";
System.out.println(message);
}
}/]

my goal is for it to say invalid entry not counted after you put in a valve greater than 100 but is not the 999 to end the program
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: question how to make this test app work

Posted 10 September 2011 - 06:05 AM

Post code in code tags.

You didn't use the if/else construct I gave you. For your last run, you should have seen "Invalid Entry" but then the program would fall through to the remaining statements as you saw.
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: question how to make this test app work

Posted 10 September 2011 - 06:10 AM

while (testScore > 100)
Should be <= 100. You want it to be 100 Inclusive and you want it to stop if it is 999. Right now that will do the opposite.

Problem right now is that no matter what number it will add it because the adding it is outside of the block:
if ( testScore < 0 || testScore > 100 ) // invalid scores 
{ 
System.out.println( "Invalid entry" ); 
} 
{
scoreCount = scoreCount + 1;
scoreTotal = scoreTotal + testScore;
}
Outside the if statement so will always perform action.

I would use an else if block for the rest of them. First you need to check if it is 999
if(testScore == 999) Make it empty if you want to, just needs to not do anything else for the loop. Could also use break; statement there to force leave the while loop. Considered Tabboo by some professors though.
else if(<Test score is in the 0-100 range inclusive>) If it is in the range perform the code:
scoreCount = scoreCount + 1;
scoreTotal = scoreTotal + testScore;

and finally an else System.out.println("Invalid Entry"); You do this to throw the invalid entry prompt if it is A. Not 999 and B. ir not 0-100

This will first check to see if they want to terminate, then will see if it is in range, if it is will perform its duties, if not 999 and not in range then it will throw the invalid entry error. Once the loop is done it will do the calculations.

Make sense?

This post has been edited by Fuzzyness: 10 September 2011 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: question how to make this test app work

Posted 10 September 2011 - 06:30 AM

To Fuzzyness' excellent point, your original while loop used the condition while (testScore < 999). That was better than what you just posted. Even better (more specific) than that would be while (testScore != 999).

This post has been edited by GregBrannon: 10 September 2011 - 06:31 AM

Was This Post Helpful? 0
  • +
  • -

#12 moparman426  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 32
  • Joined: 08-September 11

Re: question how to make this test app work

Posted 10 September 2011 - 06:31 AM

sort of makes senses but still isnt workin idk why its not ?? here is my code modified with your snipets:

[ import java.util.Scanner;

public class TestScoreApp
{
public static void main(String[] args)
{
// display operational messages
System.out.println("Please enter test scores that range from 0 to 100.");
System.out.println("To end the program enter 999.");
System.out.println(); // print a blank line

// initialize variables and create a Scanner object
double scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
Scanner sc = new Scanner(System.in);

// get a series of test scores from the user
while (testScore <= 100)
{
// get the input from the user
System.out.print("Enter score: ");
testScore = sc.nextInt();

// accumulate score count and score total
if ( testScore == 999 ) // invalid scores
else if (< testScore is in the 0-100 range inclusive>)
{
else System.out.println( "Invalid entry" );
}


{
scoreCount = scoreCount + 1;
scoreTotal = scoreTotal + testScore;
}
}

// display the score count, score total, and average score
double averageScore = scoreTotal / scoreCount;
String message = "\n" +
"Score count: " + scoreCount + "\n"
+ "Score total: " + scoreTotal + "\n"
+ "Average score: " + averageScore + "\n";
System.out.println(message);
}
} /]

i dont understand what iam doing wrong here AT ALL here is the results of running the program

Please enter test scores that range from 0 to 100.
To end the program enter 999.

Enter score: 250
Enter score: 600
Enter score: 1000

Score count: 2
Score total: 850.0
Average score: 425.0

still not getting the error message or anything what is going on ??
Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: question how to make this test app work

Posted 10 September 2011 - 06:50 AM

Code tags: select the code (highlight it) and select the code button in the editor's tools area.

Your if statement is completely off the reservation. Let's look at it piece by piece to see if we can improve your understanding. (I thought it was an understanding problem, but I'm thinking it's more of a syntax, bracketing issue. Rewrite your code:
if ( testScore == 999 ) // invalid scores
{

}
else if (< testScore is in the 0-100 range inclusive>)
{

} 
else 
}
    System.out.println( "Invalid entry" ); 
}

As it is, you have nested else statements (I think, hard to tell with malformed code and no code tags). I'm surprised you're not getting error messages, but I may be just reading it wrong.

Even so, consider the logic: Your if statement checks testScore for equality to 999. Yes, that's an invalid score, but your while statement already 'filters' out testScores greater than 100. This 'if' statement will never execute, because testScore can never be 999.

Let's continue with the if statement logic:

The clause to execute if testScore was 999 would appear right after the 'if' statement. Currently you have none. Writing it differently:
if ( testScore == 999 ) // invalid scores
{

}
else if (< testScore is in the 0-100 range inclusive>)
{ 
else System.out.println( "Invalid entry" ); 
}

you can now see that the clause between the brackets after the 'if' statement is empty. So in the if/else construct, if the if statement is not true, remember it never will be, the else clause is (or clauses are) executed. You have two else statements. The first is followed by an if statement that includes a comment. I'm not sure what's going on there. The second else should execute by default, but I don't think you've posted the actual code. If it were, I'd think you'd be getting an error.

I'd GIVE you the code, but that hasn't worked in the past. Try fixing what you have based on our comments and thinking the function and logic through.

This post has been edited by GregBrannon: 10 September 2011 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: question how to make this test app work

Posted 10 September 2011 - 06:54 AM

Also you copied and pasted my code, you need to actually fill in the limitations for the else if not just else if (< testScore is in the 0-100 range inclusive>)
The < > Means goes here kind of thing.

TO extend Gregs code, for the else need to fix the brackets.
else if( ... )
{

}
else System.out.println("Invalid Entry");

This post has been edited by Fuzzyness: 10 September 2011 - 06:56 AM

Was This Post Helpful? 0
  • +
  • -

#15 moparman426  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 32
  • Joined: 08-September 11

Re: question how to make this test app work

Posted 10 September 2011 - 07:15 AM

TO GREG ive been posting the code this is the code iam using and need to solve!! none of ur answers are working i need you TO GIVE ME THE CODE whole thing to see what your talking about and if its working because neither of you have been any help just confusing me more and its working but still no error message see your enter the score right so 90 then enter for another score should be able to hit like 250 and it say invalid entry not counted however its reading as an actual test answer here is the code again ALL OF IT :

import java.util.Scanner;

public class TestScoreApp
{
    public static void main(String[] args)
    {
        // display operational messages
        System.out.println("Please enter test scores that range from 0 to 100.");
        System.out.println("To end the program enter 999.");
        System.out.println();  // print a blank line

        // initialize variables and create a Scanner object
        double scoreTotal = 0;
        int scoreCount = 0;
        int testScore = 0;
        Scanner sc = new Scanner(System.in);

    	// get a series of test scores from the user
        while (testScore != 999)
        {
		    // get the input from the user
            System.out.print("Enter score: ");
        	testScore = sc.nextInt();

        	// accumulate score count and score total
        	if ( testScore == 999 ) // invalid scores
        	{
        	}
        	else if (< testScore is in the 0-100 range inclusive>)
               {  
               }
        	else System.out.println( "Invalid entry" );  
            }  


        	{
        		scoreCount = scoreCount + 1;
        		scoreTotal = scoreTotal + testScore;
			}
		}

    	// display the score count, score total, and average score
    	double averageScore = scoreTotal / scoreCount;
    	String message = "\n" +
    	                 "Score count:   " + scoreCount + "\n"
    	               + "Score total:   " + scoreTotal + "\n"
		               + "Average score: " + averageScore + "\n";
		System.out.println(message);
	}
}


my goal is to get to display the error message if a user enters a score GREATER THAN 100 not the thing to make it stop or exit which is 999 thats the STOP i still dont see why any of it is not working been trying your spinets and bracket replacing still no results iam using ECLISPE to build my code could that be a problem ??? idk not being mean jsut so far iam getting no results which thus makes me lost and confused
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2