Loop control for set number of times per loop

Hello everyone,
I am having a problem with an assignment, I have gotten it all to work except one thing. The assignment was to create a quiz asking for the planets in order by using JOptionPane input dialog,loop, and StringBuffer. If the user answers incorrect show a message dialog with a hint and ask the same question again. If the user gets it wrong again, then just move on to the next question. I got it to work except I cant figure out how to make it ask the question again only once if the answer is wrong. Here is what I have so far:
   import javax.swing.*;
    public class SolarSystemQuiz {
       public static void main(String[] args) {
         String answer;
         int i ;
         int count = 0;
         int correct = 0;
         int wrong = 0;
         boolean crt;
         for (i=1; i < 10; ++i) {
            StringBuffer str = new StringBuffer("What is the " + getPlace(i) + " planet in the solar system?");
            do {
               answer = JOptionPane.showInputDialog(null, str);
               crt = getAnswer(i, answer);
               if (crt) {
                  ++correct;
                  count = 0;
               else{
                  ++count;
                  ++wrong;
            } while ( ! crt && count <= 1);
         JOptionPane.showMessageDialog(null, "You got " + correct +
            " answers correct\n, and " + wrong + " incorrect answers.");
         System.out.println(correct + wrong);
         System.exit(0);
       public static String getPlace(int spot) {
         switch (spot) {
            case 1:
               return "1st";
            case 2:
               return "2nd";
            case 3:
               return "3rd";
            case 4:
               return "4th";
            case 5:
               return "5th";
            case 6:
               return "6th";
            case 7:
               return "7th";
            case 8:
               return "8th";
            case 9:
               return "9th";
            default:
               return "You have created an error, please try again.";
       public static boolean getAnswer(int planet, String guess ) {
         String mercury = "mercury" ;
         String venus = "venus";
         String earth = "earth";
         String mars = "mars" ;
         String jupiter = "jupiter";
         String saturn = "saturn";
         String uranus = "uranus";
         String neptune = "neptune";
         String pluto = "pluto" ;
         switch (planet) {
            case 1:
               if (guess.equalsIgnoreCase("Mercury")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;  
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with M and has " + mercury.length() + " letters");
                  return false;
            case 2:
               if (guess.equalsIgnoreCase("Venus")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with " + venus.charAt(0) + " and has " + venus.length() + " letters");
                  return false;
            case 3:
               if (guess.equalsIgnoreCase("Earth")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with " + earth.charAt(0) + " and has " + earth.length() + " letters");
                  return false;
            case 4:
               if (guess.equalsIgnoreCase("Mars")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with " + mars.charAt(0) + " and has " + mars.length() + " letters");
                  return false;
            case 5:
               if (guess.equalsIgnoreCase("Jupiter")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with " + jupiter.charAt(0) + " and has " + jupiter.length() + " letters");
                  return false;
            case 6:
               if (guess.equalsIgnoreCase("Saturn")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with " + saturn.charAt(0) + " and has " + saturn.length() + " letters");
                  return false;
            case 7:
               if (guess.equalsIgnoreCase("Uranus")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with " + uranus.charAt(0) + " and has " + uranus.length() + " letters");
                  return false;
            case 8:
               if (guess.equalsIgnoreCase("Neptune")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with " + neptune.charAt(0) + " and has " + neptune.length() + " letters");
                  return false;
            case 9:
               if (guess.equalsIgnoreCase("Pluto")) {
                  JOptionPane.showMessageDialog(null, "Correct!");
                  return true;
               else {
                  JOptionPane.showMessageDialog(null,
                     "not it, the planet starts with " + pluto.charAt(0) + " and has " + pluto.length() + " letters");
                  return false;
            default:
               JOptionPane.showMessageDialog(null, "Not in this universe, try again");
               return false;
   } Thanks for your time,
Brad

This was a perfect example of how fast and easy a fix can be when the OP provides sufficient information and code.

Similar Messages

  • How to loop an swf a set number of times?

    I need to loop a banner animation a set number of times, then
    get it to gotoandstop on a final frame. I know in the past I could
    use:
    counter++; if (counter<5) { gotoAndPlay (1); }
    At least for the looping part (don't even know yet about the
    stop pon a particular frame part).
    but now in Flash 8 I can't get my test swf to do that using
    this statement. Am I missing something? (Of course, obviously I am
    or I wouldn't be here!)

    This works for me as well, but i don't want it to jump to the
    final still frame. i need mine to loop three times, then plan
    through to the frame i want, stopping there. the frame i want is
    not the final frame in the swf. it is somewhere in the middle.
    does that make sense?

  • Loop though foreach a set number of times

    Hi Guys
    I want to know how I can loop though a foreach loop a set number of times ? I know in a for loop you can set the max number of loops but I don't know how to do it when comes to a foreach loop. Below is the structure of my foreach loop:
    for(Letter letter : letters) {
      //I want to loop though only 50 objects max at a time
    }

    List<Letter> prefix = (letters.size() <= limit) ? letters : letters.subList(0, limit);
    for(Letter letter : prefix) {
    }

  • Running a java program a set number of times

    This is a general question. Is it possible to make a java program run only 5 times for the sake of arguement.
    Basically I want to write a program that will give the user some flexibility when it will actually run another Java program, but I only want them to be able to say "not now' for a set number of times. When the last time comes the other program will launch. I was initially thinking of the Do Whilw loop, but this needs to work when the program is restarted.
    Program starts, it has 5 times it will run before it does something else(doesn't really matter now I think). User takes option "Not Now" and the program ends, but warns the user this will run 4 more times before you will need to do something.
    This process will repeat until the user takes the option "Ok install now" or the time limit expires and the install occurs anyway. Can someone point me in the right direction.

    ok I see so it's like one those programs that you download for free on the internet and they give you a set amount times to use it before you have to pay for it. but in this case when the number of times you use it equals 5 (or when the user clicks ok) a different java app will open automatically.
    My first thought would be to Write a Serialized object to disk using objectOutputStream that stores the number of times the application has been opened. and each time the program runs it checks for the serialized object and then you can do something like what I posted before. of course if were worried about security the user could always look for the object and erase it, if so then I guess we would have to come up with another plan of attack
    Hope this helps

  • Using skip logic to show sections of the form a set number of times

    Hi
    I have form which i need all to fill out basic information and a section which asks for furtehr information if a certain condition is true. what i'm looking ofr is this section of the form to be shown again as long as a condition is true or for x numbe rof times.
    the form collects basic information about event companies and information about one event, i have a question which asks howmany events per year do you run. if this is more than one or set to true section part of the form would need to be shown again for x number of times to collect the information about thos events also.
    If not is there any workaround i could implemnet using skip logic to acheieve an efective solution?
    thanks

    Yeah that is the work around. This post explains how:
    http://forums.adobe.com/thread/1142978?tstart=0
    Its a little tedious to setup up but it should work.
    Randy

  • Does anyone know if there is a way for the number of time a song has been played on your iPhone/iPod be transferred to the songs on your iTunes number of plays?

    I mainly just use my iTunes for organizing and adding new songs. Most of the time I listen to my music on my iPhone. I wish it would keep track of how many times I play a song in my music and transfer it to the computer under the 'number of times played' column. It would help out so much! Does anyone know if there is a way for the number of time a song has been played on your iPhone/iPod be transferred to the songs on your iTunes number of plays?

    Hi @imobl,
    You sound like an Apple support guy who hasn't been able to answer my questions.
    To respond to some of the points you made,
    - I did not ignore Ocean20's suggestion. If you has read my post, you would have known that I took my phone to the apple service centre where they tried this restore on THEIR machines. I am assuming that Apple guys know how not to block iTunes. So I actually do not understand your point about me trying the hosts file changes on my machine. Do you not believe that apple tested this issue with the correct settings?
    - you also give a flawed logic of why the issue is a hardware issue. You mentioned that If I thought that the issue was with the software, i should try a restore and getting it to work. The problem is that my error (23), and many others comes up when the restore fails. And you would be astonished to know that not all errors are hardware errors. Sometimes even software errors prevent restores. Funnily enough Apple itself mention that 'in rare cases, error 23 could be hardware related'.
    - all Apple has done so far is replicate the issue. I don not know how anyone can conclude that the issue is a hardware issue.
    And by the way, I am not certain that this is a software bug. Again if you read my Posts, you will notice I only want a confirmation,/proof that the issue is hardware related as they mention..
    Please refrain do. Responding if there is nothing to add.

  • Adobe lightroom asking for serial number each time I open it. Any suggestions?

    I just downloaded Lightroom 5 and updated to 5.7 when prompted on completion of the process. I now have to input the serial number each time I open the programme. Is there any thing I can do, restarting the installation for example, to try and get round this problem?

    I have the same thing happen to me. Every time I start Aperture, it asks for the license. Once I have entered the license key it lets me use the application. However, the next time I use the application again, i get the same problem. I have tried reinstalling the application, and am asked for the license key at the beginning of the install and again once the package is started. To no avail. And I get no other messages.
    I called Apple Support, and after booking a call with a support person (a Genius?), I got one who, after reading the summary of my problem in the service request, said simply "this is for Aperture? Aperture is a Microsoft product, you have to call Microsoft". And that was that.
    Please, anyone have any ideas?

  • Updated iPad2 to iOS6. Now it randomly freezes. Have to reboot (home   sleep key for 15 seconds) several times per day. Help!

    Help! I guess I can restore it to original factory settings but I am afraid of losing everything. If it helps the iPad is NOT jailbroken. Any ideas or do I have to wait for a future update?

    Just to clarify. Once I am done with the iPad and push the sleep button (upper right corner) or just let it sleep from inactivity for a couple minutes, the issue happens when I try to wake it up. I can see the unlock screen with the pin pad but the iPad becomes unresponsive and I cannot enter the numbers or I can enter the numbers but it freezes right after that while the pin pad is still visible. The only thing that fixes it is to hold the home and sleep button for 10-15 seconds and wait a minute for the whole thing to reboot. At that point it usually works. Still, what I liked about the iPad was the convenience of being immediately ready and now I have to reboot many times per day.
    I have an iPad2 with the latest update (6.01) and it is not jail broken. Apple help me please!

  • Already have wireless network, need help for set up of time capsule for wired backup only.

    Hello all -
    I have a time capsule that plugs into wall and used a wireless network to back up computer.  It would always mess up internet afterwards so I would unplug the time capsule and reset internet until the next back up.  my time capsule now will not link into my computer. It just flashes amber.  Spoke with apple and was told to come on here and ask for help for setting up time capsule for a wired backup only and to mention that I have a wireless network.
    Hope any of this makes sense.
    Thank You!!

    Spoke with apple and was told to come on here and ask for help
    Really?  The paid professionals at Apple told you to post on a forum where users.....just like you....are trying to answer questions?  We'll try to help, though.
    For starters, please tell us the make and model of the modem/router that you have now that is providing your wireless network.

  • Table control for equipment number in me51n

    There is a field for equipment number in me51n for entering a single value , i have to modify that screen by giving a table control so that multiple values can be entered . How do i do that ?

    any suggestions on this please

  • Logic asks for serial number every time I start it...

    Hi,
    I've just reinstalled Logic Studio and it asks me for a serial number every time I start up Logic Pro if I don't have the old XS key inserted.
    It's the upgrade version and in the past i've just needed to insert the XS Key at installation, not evey time I use it.
    I'm using Logic Pro (lastest update) with Lion which Apple says is supported.
    Can anyone help?

    I had this issue when I upgraded. I resolved it by inserting the key before I started the upgrade and left it in there and then valdidate it with the serial. After that it never asked for the key again. If this is not working you might want to contact support and they probably provide a new serial key.

  • Best Method for setting up a time-defined logout function.

    Greetings,
    I'm attempting to setup an application that is only valid between certain hours of the day. I'm able to setup a test to verify that I'm within that time frame, but I'm not sure exactly where to put this pl/sql procedure to check to see if it's within a preset time frame.
    I've placed the application within the specific page and it works fine, but as it needs to be pretty much application wide, should I put it on page zero (which I believe is executed every time) or should I setup an application level process. I've tried the application level process way, but just get thrown into an infinite loop.
    Here is the snippet of code that is doing the test and the forced logout procedure
    declare
      begintime date := to_date('11:27','HH24:MI');
      endtime   date := to_date('17:00','HH24:MI');
    BEGIN
      IF to_char(sysdate,'HH24:MI') >= to_char(begintime,'HH24:MI') THEN
        wwv_flow_custom_auth_std.logout(p_this_flow=>v('APP_ID'),p_next_flow_page_sess=>v('APP_ID')||':101');
        wwv_flow.g_unrecoverable_error := true;   end if;
    END;Is this method OK for doing what I want to do, and where should I place this function. Is there a way of setting it up as an application level process without it going into
    an infinite loop.
    Thanks in Advance,
    Cliff Moon
    UTPA Webmaster
    Message was edited by:
    cjmoon

    Cliff - I don't understand your date logic but if you get that part working, I'd recommend using the authentication scheme's Session Verification Function attribute. Put logic in there that returns false if it's not okay to be running the application at the current time (this is executed for each page view.) Otherwise the function must return true. Then the engine will redirect to the login page (and I believe the function won't run there).
    Scott

  • Design advice for setting users default time

    I have an application that in a number of different places requires that records when being updated or created are shown to default to the user's current date. Depending on the location of the user, this could be a different day than where the server is.
    All of the relevant fields in the database tables are using a datatype of "TIMESTAMP WITH TIMEZONE". When a users account is initially created, the timezone that the user is in is saved. When the user logs on, an "alter session set time_zone" command runs on the database to change the time zone of the current session.
    When a user updates or creates a record and one of the field(s) requires to display the current date of the user, I call a ViewObject which runs a query to "select current_timestamp from dual" to return the current date from the database and populate the field with this.
    I've realised that I'm creating a considerable overhead as this view object may be queried dozens of times during a users session and was thinking of running it once when the user logs on and storing the "USER_DATE" as a session variable - then I can simply refer to this each time, instead of many round-trips off to the database. Obviously there is a risk if the user logs on just before midnight and stays on until after that the date will be incorrect but this is extremely unlikely as the application is only used during normal business hours up to 9pm as an exception
    Am I heading down the right track here or doing something daft (and missing something far more simple that I should be doing !)
    Cheers,
    Brent

    Hi Frank,
    Thanks for that - after 8 years of working with Oracle Forms and afterwards the same again with ADF, I still find it hard sometimes when using ADF to understand the best approach to a particular problem - there is so many different ways of doing things/where to put the code/how to call it etc... ! Things seemed so much simplier back in the Forms days !
    Chandra - thanks for the information but this doesn't suit my requirements - I originally went down that path thinking/expecting it to be the holy grail but ran into all sorts of problems as it means that the dates are always being converted into users timezone regardless of whether or not they are creating the transaction or viewing an earlier one. I need the correct "date" to be stored in the database when a user creates/updates a record (for example in California) and this needs to be preserved for other users in different timezones. For example, when a management user in London views that record, the date has got to remain the date that the user entered, and not what the date was in London at the time (eg user entered 14th Feb (23:00) - when London user views it, it must still say 14th Feb even though it was the 15th in London at the time). Global settings like you are using in the adf-config file made this difficult. This is why I went back to stripping all timezone settings back out of the ADF application and relied on database session timezones instead - and when displaying a default date to the user, use the timestamp from the database to ensure the users "date" is displayed.
    Cheers,
    Brent

  • Authorization Control for Setting TECO

    Hi PS Gurus,
    Would like to ask if there's a standard way of controlling users on the setting of TECO for PS objects? I cant find any setting in T-code PFCG that may limit setting of TECO for selected roles. Suggestions outside standard is also welcome...
    Thanks in advance.
    Joed

    Hi Abdul,
    Thanks for the answer, I have already taken that into consideration, however my problem will be assigning the Status Profiles for all existing WBS Elements (around 10,000+ WBSE's).
    The status control will solve future WBSE's but we were also asked to consider existing ones.
    Thanks again.
    Cheers,
    Joed

  • Lr4 asks for serial number every time it starts

    I purchased a Lightroom 4 upgrade license from the Adobe store and I can't get the serial number to stick after entering it in the start-up dialog. Every time I launch Lightroom, the application asks for the new serial number and the original serial number for the product that I upgraded from (Lr3). I get green checks when I enter both numbers and Lightroom functions normally, but when I quit and launch Lightroom again it starts from scratch and I need to re-enter both serial numbers.
    I'm running Mac OS X 10.7.3

    A thread associated with a Lr2 upgrade has a solution. I deleted the file 'Lightroom 4.0 Registration' at /Library/Application Support/Adobe/Lightroom (NB: not the user's Library) and I am no longer prompted for the serial number. Yeah!

Maybe you are looking for

  • Problem with printing and scanning after updating to Mavericks for my Photosmart C7180

    After updating my macbook pro (mid 2009) to Mavericks, I have found that I can only scan to a jpeg2000 and cannot scan as a tiff or jpeg or anyother format.  I have followed instructions on troubleshooting and have not been able to make anything work

  • How do i sync my phone to a new itunes without loosing everything?

    I had to have my laptop restored as it was broken which meant everything was wiped so i had to reinstall itunes. When i plugged my phone in expecting it to be fine it wouldnt sync with my laptop without wiping the contents and replacing it with the c

  • Controlling root time line from a movie clip

    Hi Ive been searching around for an hour or so and cant find anything that helping. Im using cs4 and actionscript 2. in my 2nd scene i have a movie clip with a roll over button inside, i need that button to control the root timeline and navigate arou

  • Hi I have a geometry problem.Can anyone help with java code

    Hi I have a non simple polygon . My job is to make it a simple. for that First I need to find the intersecting lines say line AB intersects line CD. If so then I must replace AB & CD with AC & BD or AD or BC which ever keeps the polygon closed . So c

  • Ignoring extra characters...

    Hi, I'm creating a method where when you input a number in the String it ignores the extra characters. For example...if i enter "-4.414E-80EEE", i should get "-4.414E-80", but i get "-41480.0". Here's my code:    public double convertDouble1 (String