Your Ad Here Your Ad Here
Results 1 to 5 of 5

Thread: Java Programmers..?

  1. #1
    i'm learning java at uni just now and usually i prepare my program before the practical.
    normally, myself and TClite try to program at the same time, conversing on msn messenger until one of us finishes first (him without exception )
    so this week, i think it only fair to cheat

    anyway, does anyone know how i would insert a loop into the following program so that the program would ask for another country until the user presses the "cancel" button?

    a very big thanks in advance too

    /**
    * CheeseSwitch
    *
    *


    * Choose a country using ListBox and a Switch Statement
    *
    * @author
    */

    import javabook.*;

    public class CheeseSwitch
    {
    public static void main (String[] args)
    {
    MainWindow mainWindow = new MainWindow("Country Chooser");
    MessageBox messageBox = new MessageBox( mainWindow );

    ListBox countryList = new ListBox( mainWindow, "Select Country");

    int choice;

    mainWindow.setVisible(true);

    countryList.addItem("Scotland");
    countryList.addItem("England");
    countryList.addItem("France");
    countryList.addItem("Greece");
    countryList.addItem("Netherlands");

    choice = countryList.getSelectedIndex();

    switch ( choice )
    {

    case ListBox.NO_SELECTION :
    messageBox.show("No selection made");
    break;

    case ListBox.CANCEL:
    messageBox.show("Cancelled !");
    break;

    case 0 :
    messageBox.show("Scottish Cheese");
    break;

    case 1 :
    messageBox.show("English Cheese");
    break;

    case 2 :
    messageBox.show("French Cheese");
    break;

    case 3 :
    messageBox.show("Greek Cheese");
    break;

    case 4 :
    messageBox.show("Dutch Cheese");
    }
    }
    }


    p.s. sorry in advance for the indentation, for some reason the board software likes to mess it up
    <span style='font-size:14pt;line-height:100%'>BLAH</span>

    <span style='font-size:14pt;line-height:100%'>Wayne Rooney - A thug and a thief</span>

  2. Software & Hardware   -   #2
    Here is the modified code for it. You don&#39;t need to show mainWindow, showing message box is enough as you are only using message box and listbox. So I got rid of it. If you want you can add that. Doesnt make a difference only brings an empty window in the background of your listbox.

    The rest of the code is clear itself. Feel free to ask.

    Code:
    import javabook.*;
    
    public class CheeseSwitch{
    	
    
    	public static void main &#40;String&#91;&#93; args&#41;{
     *
     *MainWindow mainWindow = new MainWindow&#40;&#34;Country Chooser&#34;&#41;;
     *MessageBox messageBox = new MessageBox&#40; mainWindow &#41;;
    
     *int choice =0;
    
     *while&#40;choice&#33;=ListBox.CANCEL&#41;{
     *	ListBox countryList = new ListBox&#40; mainWindow, &#34;Select Country&#34;&#41;;
     *	countryList.addItem&#40;&#34;Scotland&#34;&#41;;
     *	countryList.addItem&#40;&#34;England&#34;&#41;;
     *	countryList.addItem&#40;&#34;France&#34;&#41;;
     *	countryList.addItem&#40;&#34;Greece&#34;&#41;;
     *	countryList.addItem&#40;&#34;Netherlands&#34;&#41;;
    
     *	choice = countryList.getSelectedIndex&#40;&#41;;
    
    
     *	switch &#40; choice &#41;{
    
     * *case ListBox.NO_SELECTION &#58;
     * *messageBox.show&#40;&#34;No selection made&#34;&#41;;
     * *break;
     *	
     * *case 0 &#58;
     * *messageBox.show&#40;&#34;Scottish Cheese&#34;&#41;;
     * *break;
    
     * *case 1 &#58;
     * *messageBox.show&#40;&#34;English Cheese&#34;&#41;;
     * *break;
    
     * *case 2 &#58;
     * *messageBox.show&#40;&#34;French Cheese&#34;&#41;;
     * *break;
    
     * *case 3 &#58;
     * *messageBox.show&#40;&#34;Greek Cheese&#34;&#41;;
     * *break;
    
     * *case 4 &#58;
     * *messageBox.show&#40;&#34;Dutch Cheese&#34;&#41;; 
     *	}
    
     *}
    
     *//Here choice==ListBox.CANCEL
     *messageBox.show&#40;&#34;Cancelled&#34;&#41;;
    	}
    
    }
    Hope this helps.
    I.am

    Edit: Let me know if thats what you needed.
    <span style='color:black'> I am a part of all that I have met - Lord Tennyson</span>
    <span style='color:blue'>Try not to let your mind wander...it is too small and fragile to be out by itself</span>

  3. Software & Hardware   -   #3
    thank you very much

    i hope i can return the favour some time...

    the only reason i was using the mainwindow was because it was included in the template we were given...i see now that it&#39;s not needed
    <span style='font-size:14pt;line-height:100%'>BLAH</span>

    <span style='font-size:14pt;line-height:100%'>Wayne Rooney - A thug and a thief</span>

  4. Software & Hardware   -   #4
    No Problem. btw doesnt that Wu Textbook sucks.
    <span style='color:black'> I am a part of all that I have met - Lord Tennyson</span>
    <span style='color:blue'>Try not to let your mind wander...it is too small and fragile to be out by itself</span>

  5. Software & Hardware   -   #5
    Originally posted by I.am@1 December 2003 - 03:40
    No Problem. btw doesnt that Wu Textbook sucks.
    well, i&#39;ve not bought it yet
    i keep on meaning too, but the agenda always turns to pool...

    i&#39;ve got a 100% record so far in programming anyway, so i&#39;m not doing badly, at the moment

    thanks again though
    <span style='font-size:14pt;line-height:100%'>BLAH</span>

    <span style='font-size:14pt;line-height:100%'>Wayne Rooney - A thug and a thief</span>

Bookmarks

Posting Permissions

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