Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class BadmintonGame {
- public static void main(String[] args){
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter name of first player: ");
- String first = scanner.nextLine();
- System.out.print("Enter name of second player: ");
- String second = scanner.nextLine();
- int score;
- int score1;
- int total1 = 0;
- int score2;
- int total2 = 0;
- int ServedNext=0;
- String name="";
- while(total1<30||total2<30){
- if(total1==0&&total2==0){
- score = (int)(2*Math.random());
- if(score==1){
- total1++;
- System.out.printf("%s served: %d vs. %d\n",first,total1,total2);
- ServedNext=1;}
- else{
- total2++;
- System.out.printf("%s served: %d vs. %d\n",first,total1,total2);
- ServedNext=2;}
- }
- if(total1>0||total2>0){
- score = (int)(2*Math.random());
- if(score==1){
- total1++;
- if(ServedNext==1){
- name = first;
- }else{
- name = second;
- }
- System.out.printf("%s served: %d vs. %d\n",name,total1,total2);
- ServedNext=1;}
- else if(score!=1){
- total2++;
- if(ServedNext==2){
- name = second;
- }else{
- name = first;
- }
- System.out.printf("%s served: %d vs. %d\n",name,total1,total2);
- ServedNext=2;
- }
- if(total1<20&&total2==21)
- break;
- if(total1==21&&total2<20)
- break;
- if(total1>=20&&total2>=20){
- if(total1-total2==2||total2-total1==2)
- break;
- if(total1==30||total2==30)
- break;
- }
- }
- }
- if(total1>total2)
- System.out.printf("%s wins.",first);
- else
- System.out.printf("%s wins.",second);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement