Results 1 to 3 of 3
Thread: Working with a switch
- 03-11-2011, 11:45 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Working with a switch
I'm using a switch statement to handle a process. The method is called handle() and I'm having trouble getting to the right place when I want to end it. It's part of a exercise I did in school a while back. This bank was made to handle one customer in the exercise, now I'd like to expand it to be able to handle multiple customers.
I run this bank from a main class:Java Code:public void handle() //Metod given av uppgiften. { int choice; Scanner sc = new Scanner(System.in); while (true) { System.out.println(""); System.out.println("******************************************"); System.out.println("* Hanterar kund " + customerNumber + " (" + person.getName() + ")" ); System.out.println("* (0) Avsluta"); System.out.println("* (1) Lista konton"); System.out.println("* (2) Skapa konto"); //etc... System.out.print("* Ditt val: "); if (sc.hasNextInt()) { choice = sc.nextInt(); } else { choice = -1; } sc.nextLine(); // Läs förbi radslutet switch (choice) { case 0: break; //This is the choice that should end it, or better let the user choose an other customer. case 1: a_method(); break; case 2: an_other_method(); break; //etc... } } }
The idea now was to add a while loop that runs the bank as long as customer -1 isn't picked. I'd be thrilled if anyone would help me out.Java Code:while(customer != -1) //Programmet avslutas om användaren väljer -1. { System.out.println("***********************************************************"); //Skapa en tjusig tabell. System.out.println("* Välj Kundnr. Namn"); System.out.println("***********************************************************"); int numberCostumers = costumers.length; //iterera igenom de kunder som finns. for(int i = 0; i < numberCostumers; i++) { System.out.println("* (" + i + ") " + costumers[i].getCustomerNumber() + " "+ costumers[i].getPerson().getName()); //skriv ut kunder i listan } System.out.println("***********************************************************"); //ge användaren alternativ. System.out.println("* Välj en person att hantera, använd siffrorna under välj. "); System.out.println("* Avsluta programmet genom att välja -1. "); System.out.println("***********************************************************"); Scanner sc = new Scanner(System.in); //öppna för scanning. System.out.print("Välj kund: "); int valdKund = sc.nextInt(); //läs in användarens val. customer = valdKund; //sätt customer för att se in while-loopen ska fortsätta köras. while( (valdKund < 0 ) || (valdKund >= numberCostumers)) //Om användaren väljer felaktigt. { System.out.println("* Välj bland de kunder som finns med i listan."); System.out.print("Välj kund: "); valdKund = sc.nextInt(); if(valdKund == -1) //Se till att användaren kan avsluta programmet inne i while-loopen. { System.out.println("Programmet avslutas"); System.exit(0); //Testar System.exit(0); } } costumers[valdKund].handle(); //hantera den valda kunden. }
- 03-11-2011, 01:00 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Spam reported.
- 03-12-2011, 12:47 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
\n not working in GUI (working code, but \n isn't working)
By cc11rocks in forum New To JavaReplies: 2Last Post: 01-04-2011, 04:30 AM -
switch
By dj kourampies in forum New To JavaReplies: 17Last Post: 01-30-2009, 05:32 PM -
switch
By dj kourampies in forum New To JavaReplies: 2Last Post: 01-30-2009, 08:46 AM -
Switch help please!!!!
By soc86 in forum New To JavaReplies: 6Last Post: 11-23-2008, 07:25 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks