OrangeSoul Posted July 8, 2004 Share Posted July 8, 2004 if ( scor1 > scor2 ){ oneCount++; System.out.println(team1 + " Won the game and series is " + oneCount + ":" + twoCount);} else{ twoCount++; System.out.println(team2 + " Won the game and series is " + oneCount + ":" + twoCount);} i have the following statement what code do i have to use to make the program print out team1 or team2 has Won when the number of won games reaches 4 times Link to comment Share on other sites More sharing options...
0 +virtorio MVC Posted July 8, 2004 MVC Share Posted July 8, 2004 if (oneCount >= 4) System.out.println("Team 1 has won"); else if (twoCount >= 4) System.out.println("Team 2 has won"); else if ( scor1 > scor2 ) { oneCount++; System.out.println(team1 + " Won the game and series is " + oneCount + ":" + twoCount); } else { twoCount++; System.out.println(team2 + " Won the game and series is " + oneCount + ":" + twoCount); } I think thats what you mean Link to comment Share on other sites More sharing options...
0 OrangeSoul Posted July 8, 2004 Author Share Posted July 8, 2004 Enter the Name of the first team:Toronto Enter the Name of the Second team:Calgary The Score for Toronto is 112 The Score for Calgary is 94 Toronto Won the game and series is 1:0 ok i get this result, but ti doesnt go to 4 games. it stops at 1 Link to comment Share on other sites More sharing options...
0 +virtorio MVC Posted July 8, 2004 MVC Share Posted July 8, 2004 You might want to upload all of your code. Link to comment Share on other sites More sharing options...
0 OrangeSoul Posted July 8, 2004 Author Share Posted July 8, 2004 import java.util.Random; import java.util.List; import cs1.Keyboard; public class Sportsim { public static void main (String[] args) { Random generator = new Random(); int scor1; int scor2; int oneCount = 0; int twoCount = 0; String team1, team2; System.out.print("Enter the Name of the first team:"); team1 = Keyboard.readString(); System.out.print("Enter the Name of the Second team:"); team2 = Keyboard.readString(); scor1 = generator.nextInt(65) + 65; System.out.println("The Score for " + team1 + " is " + scor1); scor2 = generator.nextInt(65) + 65; System.out.println("The Score for " + team2 + " is " + scor2); if ( scor1 > scor2 ){ oneCount++; System.out.println(team1 + " Won the game and series is " + oneCount + ":" + twoCount);} else{ twoCount++; System.out.println(team2 + " Won the game and series is " + oneCount + ":" + twoCount);} } } Link to comment Share on other sites More sharing options...
0 NullPointerException Posted July 8, 2004 Share Posted July 8, 2004 Do something like this (it is not pretty but it will do) while(true) { if (oneCount >= 4) { System.out.println("Team 1 has won"); break; } else if (twoCount >= 4) { System.out.println("Team 2 has won"); break; } else if ( scor1 > scor2 ) { oneCount++; System.out.println(team1 + " Won the game and series is " + oneCount + ":" + twoCount); } else { twoCount++; System.out.println(team2 + " Won the game and series is " + oneCount + ":" + twoCount); } } Link to comment Share on other sites More sharing options...
0 OrangeSoul Posted July 8, 2004 Author Share Posted July 8, 2004 nullpoint is there anyway the scores of the games can be replayed in game 1 2, 3 and so on so it shows game scores AND then increments the games won? EDIT: nullpoint after a team wins first games it just shows: Enter the Name of the first team:Test Enter the Name of the Second team:Testing The Score for Test is 80 The Score for Testing is 123 Testing Won the game and series is 0:1 Testing Won the game and series is 0:2 Testing Won the game and series is 0:3 Testing Won the game and series is 0:4 Team 2 has won Link to comment Share on other sites More sharing options...
0 NullPointerException Posted July 8, 2004 Share Posted July 8, 2004 Sure, just do a System.out.println with the current values of oneCount and twoCount before u increment the value. I think that is what u want Link to comment Share on other sites More sharing options...
0 mrvc Posted July 8, 2004 Share Posted July 8, 2004 oneCount = 0; twoCount = 0; while (oneCount < 4 && twoCount < 4) { scor1 = generator.nextInt(65) + 65; System.out.println("The Score for " + team1 + " is " + scor1); scor2 = generator.nextInt(65) + 65; System.out.println("The Score for " + team2 + " is " + scor2); if ( scor1 > scor2 ) { oneCount++; System.out.println(team1 + " Won the game and series is " + oneCount + ":" + twoCount); } else { twoCount++; System.out.println(team2 + " Won the game and series is " + oneCount + ":" + twoCount); } } if (oneCount == 4) System.out.println("\n" + team1 + " has won!"); else System.out.println("\n" + team2 + " has won!"); Link to comment Share on other sites More sharing options...
0 NullPointerException Posted July 8, 2004 Share Posted July 8, 2004 you just have to ask for a new score inside the while(true) loop and before the if (scor1 > scor2) statement. Otherwise it will assume that the score for every game is the same as the one u initialy entered (btw. forgive my english :p) Link to comment Share on other sites More sharing options...
0 mrvc Posted July 8, 2004 Share Posted July 8, 2004 you just have to ask for a new score inside the while(true) loop and before theif (scor1 > scor2) statement. Otherwise it will assume that the score for every game is the same as the one u initialy entered (btw. forgive my english :p) which is what I did above ;) Link to comment Share on other sites More sharing options...
0 NullPointerException Posted July 8, 2004 Share Posted July 8, 2004 which is what I did above ;) Yup :) Link to comment Share on other sites More sharing options...
Question
OrangeSoul
if ( scor1 > scor2 ){
oneCount++;
System.out.println(team1 + " Won the game and series is " + oneCount + ":" + twoCount);}
else{
twoCount++;
System.out.println(team2 + " Won the game and series is " + oneCount + ":" + twoCount);}
i have the following statement
what code do i have to use to make the program print out team1 or team2 has Won when the number of won games reaches 4 times
Link to comment
Share on other sites
11 answers to this question
Recommended Posts