java 双循环是如何执行的_java – 双循环赛

我正在基于循环调度算法开发

java中的运动项目.对于n支队伍,我希望用n / 2场比赛产生2(n-1)轮.也就是说每支球队都必须在一轮比赛中进行比赛,每两支球队会两次,一次一次,一次回家.我设法实现了algoritm,除了home / away部分.我能够产生回合,但是在下半场不能“交换”球队,所以他们可以在主场和主场比赛.

这是我到目前为止:

import java.util.Arrays;

import java.util.Scanner;

public class sports {

public static void main(String[] args) {

//obtain the number of teams from user input

Scanner input = new Scanner(System.in);

System.out.print("How many teams should the fixture table have?");

int teams;

teams = input.nextInt();

// Generate the schedule using round robin algorithm.

int totalRounds = (teams - 1)*2;

int matchesPerRound = teams / 2;

String[][] rounds = new String[totalRounds][matchesPerRound];

for (int round = 0; round < totalRounds; round++) {

for (int match = 0; match < matchesPerRound; match++) {

int home = (round + match) % (teams - 1);

int away = (teams - 1 - match + round) % (teams - 1);

// Last team stays in the same place while the others

// rotate around it.

if (match == 0) {

away = teams - 1;

}

// Add one so teams are number 1 to teams not 0 to teams - 1

// upon display.

rounds[round][match] = ("team " + (home + 1) + " plays against team " + (away + 1));

}

}

// Display the rounds

for (int i = 0; i < rounds.length; i++) {

System.out.println("Round " + (i + 1));

System.out.println(Arrays.asList(rounds[i]));

System.out.println();

}

}

}

不要介意偶数/奇数的球队,现在我只对球队号码感兴趣.

任何帮助表示赞赏,谢谢:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值