-
turning case to methods
hi guys im having trouble turing my cases into a method each how would sumone doo this could you show me with this example which im having trouble at.
thanks alot
finished: while (true) {
System.out.println();
System.out.println("~~~~~~~~~~~~~~~~");
System.out.println("Menu");
System.out.println("~~~~~~~~~~~~~~~~");
System.out.println("1 - List dogs");
System.out.println("2 - List players");
System.out.println("3 - Race");
System.out.println("4 - Quit");
System.out.println("");
try {
System.out.println("Make a choice (1-4): ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
Op = Integer.parseInt(s);
} catch (Exception e) {
Op = 0;
}
switch (Op) {
case 1:
System.out.println();
System.out.println(dl);
System.out.println("Dogs");
System.out.printf("%s\n%20s run 1st 2nd 3rd\n%s\n", dl, "Dog", dl);
for (i = 0; i < 20; i++) {
System.out.printf("%20s %03d %03d %03d %03d\n", d[i], rcd[i],
_1[i], _2[i], _3[i]);
}
System.out.println();
break;
case 2:
System.out.println(player);
System.out.println("Players");
System.out.println(player);
System.out.printf("%30s Cash Wins\n", "Player");
count = 0;
for (i = 0; i < nop; i++) {
s = i < h ? "human" : "computer" + aileveltype[count++];
System.out.printf("%30s £%-6d %3d", p[i] + "(" + s + ")",
cash[i], playerWins[i]);
System.out.println();
}
break;
case 3:
int racers[] = new int[5];
int odds[] = new int[5];
int totalWins = 0;
for (int m : new int[] { 0, 1, 2, 3, 4 }) {
lab: while (true) {
int rac = (int) (Math.random() * 20);
for (int x = 0; x < m; x++) {
if (rac == racers[x]) {
continue lab;
}
}
totalWins += _1[rac];
racers[m] = rac;
break;
}
}
for (int m : new int[] { 0, 1, 2, 3, 4 }) {
int od = 1 + ((int) (Math.random() * 2)) + (int)((totalWins
- _1[racers[m]] )/(double)totalWins);
odds[m] = od;
}
int[] stakes = new int[nop];
char[] dllp = new char[46];
Arrays.fill(dllp, '~');
String dlp = new String(dllp);
System.out.println();
System.out.println(dlp);
System.out.println("Dogs");
System.out.printf("%s\n %20s run 1st 2nd 3rd odds\n%s\n", dlp,
"Dog", dlp);
for (i = 0; i < 5; i++) {
System.out.printf("%d) %20s %03d %03d %03d %03d %3d/1\n", i+1,
d[racers[i]], rcd[racers[i]], _1[racers[i]],
_2[racers[i]], _3[racers[i]], odds[i]);
}
int choices[] = new int[nop];
j = 0;
for (i = 0; i < h; i++) {
while (true) {
try {
System.out.println(p[i]
+ " please enter the dog number to back?");
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
String s = in.readLine();
choices[j] = Integer.parseInt(s);
if (!(choices[j] > 0 && choices[j] <= 5)) {
throw new BadPaddingException(null);
}
choices[j]--;
break;
} catch (Exception e) {
System.out.println("Bad dog number ");
}
}
while (!false) {
try {
System.out.println(p[i] + " please enter the stake amount?");
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
s = in.readLine();
stakes[j] = Integer.parseInt(s);
if (stakes[j] < 1 || stakes[j] > cash[j]) {
throw new BadLocationException(null, 73);
}
cash[j]-=stakes[j];
j++;
break;
} catch (Exception e) {
System.out.println("Bad stake amount");
}
}
}
int chc = 876;
k = 0;
int stk = 765;
for (i = h ; i < nop ; i++) {
switch (aileveltype[k++]) {
case 1:
chc = (int) (Math.random() * 5);
stk = 1 + (int) (Math.random() * cash[i]);
System.out.println(p[i] + " selected " + chc
+ " with stakes " + stk);
break;
case 2:
chc = (int) (Math.random() * 5);
stk = 1 + (int) (cash[i] / 2);
System.out.println(p[i] + " selected " + chc
+ " with stakes " + stk);
break;
case 3:
chc = (int) (Math.random() * 5);
stk = 1 + (int) (cash[i] / 4);
System.out.println(p[i] + " selected " + chc
+ " with stakes " + stk);
break;
}
choices[i] = chc;
stakes[i] = stk;
cash[i]-=stakes[i];
}
System.out.println("Ready to race");
int sum = 0;
int cumm[] = new int[5];
for (k = 0; k < 5; k++) {
sum = sum + speed[racers[k]];
cumm[k] = sum;
}
boolean fished = false;
int pos[]=new int[5];
Arrays.fill(pos, 0);
for (int m : new int[] { 0, 1, 2, 3, 4 }) {
rcd[racers[m]]++;
}
int t = 1;
while(!fished){
Thread.sleep(500);
int rand = (int) (Math.random() * cumm[5-1]);
for (k = 0; k < 5; k++) {
if (cumm[k] >= rand ) {
break;
}
}
System.out.print("after " + t++ + " seconds ");
System.out.println("dog " + (k + 1) + " advances");
pos[k]++;
for(int lane=0;lane<5;lane++){
char spce [] = new char[pos[lane]];
String spac = new String(spce);
System.out.println(spac + (lane+1));
}
if(pos[k]==10)
fished = true;
}
System.out.println("Dog " + (k+1) + " " + d[racers[k]] + " won with odds "
+ odds[k] + "/1");
int second = -1;
int secondV = -1;
for(int s=0;s<5;s++){
if(pos[s]>secondV && s!= k){
second = s; secondV = pos[s];
}
}
int third = -1;
int thirdV = -1;
for(int s=0;s<5;s++){
if(pos[s]>thirdV && s!= k && s!= second){
third = s; thirdV = pos[s];
}
Similar Threads
-
Replies: 1
Last Post: 10-01-2006, 05:08 PM
-
By Dark Rain in forum Java
Replies: 8
Last Post: 09-30-2005, 06:42 PM
-
By VanDam in forum Database
Replies: 0
Last Post: 07-09-2005, 04:18 AM
-
By Martin in forum VB Classic
Replies: 22
Last Post: 12-03-2001, 03:53 AM
-
By Yoel Martinez in forum VB Classic
Replies: 1
Last Post: 12-12-2000, 03:17 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks