Results 1 to 4 of 4
  1. #1
    stonewallz is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Question Need help with a "Continue? (y/n)"

    Hey guys,

    First off, thank you for coming here to help me! Secondly, I'm terrible at coming up with titles, so I hope that got your attention! I'm going to keep this short n simple to avoid confusion...here is my entire code:

    -------------------------------------------------------------------------------------------

    import java.util.Scanner;
    import java.text.NumberFormat;

    public class TestScoreApp
    {

    public static void main(String[] args)
    {
    int scoreTotal = 0;
    int scoreCount = 0;
    double averageScore = 0;

    Scanner sc = new Scanner(System.in);
    String choice = "y";
    while (choice.equalsIgnoreCase("y"))
    {
    // get the input from the user
    int testScore = getIntWithinRange (sc, "Enter score: ", 0, 100);
    scoreCount += 1;
    scoreTotal += testScore;

    averageScore = (double) scoreTotal / (double) scoreCount;

    // see if the user wants to enter more test scores and discard any other value
    System.out.print ("Continue? (y/n): ");
    choice = sc.next ();



    }

    NumberFormat number = NumberFormat.getNumberInstance();
    number.setMaximumFractionDigits(1);
    String message = "\n" +
    "Score count: " + scoreCount + "\n"
    + "Score total: " + scoreTotal + "\n"
    + "Average score: " + number.format(averageScore) + "\n";
    System.out.println(message);
    }

    public static int getIntWithinRange(Scanner sc, String prompt,
    int min, int max)
    {
    int i = 0;
    boolean isValid = false;
    while (isValid == false)

    {
    i = getInt(sc, prompt);
    if (i <= min)
    System.out.println(
    "Error! Number must be greater than " + min + ".");
    else if (i >= max)
    System.out.println(
    "Error! Number must be less than " + max + ".");
    else
    isValid = true;
    }
    return i;
    }

    public static int getInt(Scanner sc, String choice)
    {
    int i = 0;
    boolean isValid = false;
    while (isValid == false)
    {
    System.out.print(choice);
    if (sc.hasNextInt())
    {
    i = sc.nextInt();
    isValid = true;
    }
    else
    {
    System.out.println("Error! Invalid integer value. Try again.");
    }
    sc.nextLine(); // discard any other data entered on the line
    }
    return i;
    }
    }
    -------------------------------------------------------------------------------------------

    Now, I've been asked to write code that discards any extra entries at the prompt that asks if you want to enter another score (I put it in bold). The book tutorial I'm using says I can do this easily with an if statement but in another section it says you can use a while loop. Basically, if the user enters in something other than "y" or "n", such as "y7" which is incorrect then the system needs to discard that input and display a message saying it was invalid and then asks again if the user wants to continue.

    I'm hoping one of you smart folks out there have the ability to read over the code and guide me in the right direction as I've been trying to code in "if statements" for a few hours now to no avail! lol I know it's probably something extremely basic but I can't comprehend it!

    Thanks for your time.

  2. #2
    stonewallz is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need help with a "Continue? (y/n)"

    Any ideas? :D

  3. #3
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: Need help with a "Continue? (y/n)"

    this is a simple way that is very efficient. it is called a "do-while" loop.
    for instance:
    Java Code:
    do{
    System.out.print(BLAH);
    x++
    }
    while( x < 5 );
    i know this is different code but you could easily make the while like != y or something like that.
    whiles are best used with booleans because the escape is as easy a "" = false; to cut out. hope that helps.

  4. #4
    stonewallz is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need help with a "Continue? (y/n)"

    Quote Originally Posted by leeroijenkiins View Post
    this is a simple way that is very efficient. it is called a "do-while" loop.
    for instance:
    Java Code:
    do{
    System.out.print(BLAH);
    x++
    }
    while( x < 5 );
    i know this is different code but you could easily make the while like != y or something like that.
    whiles are best used with booleans because the escape is as easy a "" = false; to cut out. hope that helps.
    Ill give it a shot.. Thank you very much for your time! :)

Similar Threads

  1. "Continue Yes or No" Problem
    By LetsG0Blue in forum New To Java
    Replies: 10
    Last Post: 02-17-2013, 12:27 AM
  2. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  3. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  4. Why do we need "continue" in this piece of code?
    By tariqm in forum New To Java
    Replies: 4
    Last Post: 04-08-2012, 05:47 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •