您好我一直在到處找,確定在指定的內容被認爲是不確定的:https://open.kattis.com/problems/rockpaperscissors剪刀石頭布kattis:測試用例
我通過第一次測試,但未能保持第二。我原本以爲我沒有通過第二次測試,因爲我只允許兩名球員參加比賽,我們必須允許多名球員加入。
我修正了這個問題,但仍未通過第二次測試。
我把我認爲是未定義的但我完全卡住了。
所以問:有人可以給我一個測試用例,讓我朝正確的方向發展嗎?
我不是要求代碼,而是測試用例,如果您注意到我在代碼中輸入了錯誤信息,請告知這是否是導致我的問題的原因。
import java.io.*;
import java.util.*;
public class RockPaperScissor {
/**Conditions******
*
*/
public static void main(String[] args) {
/************User Input************/
Scanner sc = new Scanner(System.in);
/*************Variables***************/
String Player1;
String Player2;
String game="";
String playerGame=sc.nextLine();
//System.out.println("PlayerA= " +numOfGames);
//System.out.println(game);
/**run through games until game=0;**/
do{
String[] playerGameSplit = playerGame.split("\\s+");
int numOfPlayers=Integer.parseInt(playerGameSplit[0]);
double numOfGames=(Integer.parseInt(playerGameSplit[1])*numOfPlayers*(numOfPlayers-1))/2;
// double player1Win=0, player2Win=0, player1Loss=0, player2Loss=0;
Integer[] player = new Integer[numOfPlayers];
Integer[] playerLoss = new Integer[numOfPlayers];
for(int x=0; x<player.length;x++){
player[x]=0;
playerLoss[x]=0;
}
/*****Runs Rock Paper Scissors Game*****/
for (int x = 0; x<numOfGames; x++) {
/**Starts first game**/
game=sc.nextLine();
String[] splitter = game.split("\\s+");
if(compare(splitter[1],splitter[3])==1){
player[Integer.parseInt(splitter[0])-1]=player[Integer.parseInt(splitter[0])-1]+1;
playerLoss[Integer.parseInt(splitter[2])-1]=playerLoss[Integer.parseInt(splitter[2])-1]+1;
}else if(compare(splitter[1],splitter[3])==-1){
player[Integer.parseInt(splitter[2])-1]=player[Integer.parseInt(splitter[2])-1]+1;
playerLoss[Integer.parseInt(splitter[0])-1]=playerLoss[Integer.parseInt(splitter[0])-1]+1;
}
}
/***Prints Required OutPut***/
for(int x=0; x<player.length;x++){
//if(player[x]==0&&playerLoss[x]==0){
////double ratio= player[x]/(player[x]+playerLoss[x]);
//System.out.println("-");
//}else
if(Double.isInfinite((double)player[x]/(double)(player[x]+playerLoss[x]))){
//double ratio= player[x]/(player[x]+playerLoss[x]);
System.out.println("-");
}else if(Double.isNaN((double)player[x]/(double)(player[x]+playerLoss[x]))){
System.out.println("-");
}else{
double ratio= (double)player[x]/(double)(player[x]+playerLoss[x]);
System.out.printf("%.3f", ratio);
System.out.println();
}
}
System.out.println();
/**moves Game to next one**/
playerGame=sc.nextLine();
}while(playerGame.matches("[\\d][\\s][\\d]"));
//System.out.println("do we make here");
}
/*********METHODS*******/
public static Integer compare(String Player1,String Player2){
//System.out.println(Player1+ " "+ Player2);
if(Player1.matches("rock")&&Player2.matches("scissors")){
return 1;
}else
if(Player2.matches("rock")&&Player1.matches("scissors")){
return -1;
}else
if(Player1.matches("paper")&&Player2.matches("rock")){
return 1;
}else
if(Player2.matches("paper")&&Player1.matches("rock")){
return -1;
}else
if(Player1.matches("scissors")&&Player2.matches("paper")){
return 1;
}else
if(Player2.matches("scissors")&&Player1.matches("paper")){
return -1;
}else
return 0;
}
}
/***********Sample Input*************/
/**
2 4
1 rock 2 paper
1 scissors 2 paper
1 rock 2 rock
2 rock 1 scissors
2 1
1 rock 2 paper
3 1
1 rock 2 paper
3 rock 2 paper
1 rock 3 paper
0
**/
/***********Sample Output*************/
/**
0.333
0.667
0.000
1.000
**/
到目前爲止,您是否有過任何測試用例?另外,爲了使測試更容易,請嘗試使用更多功能。例如,你可以有一個函數解析playerGameSplit並設置2個變量「numOfPlayers」和「numOfGames」。然後,您可以輕鬆地測試該功能,並在轉到下一部分之前確定該代碼段是否正常工作。 – PunDefeated
這些在線代碼提交從未使用過對象嗎?我還沒有看到很多(超出我自己)那些模擬玩家或回合或錦標賽的模型。如果你做了這些事情,玩家的平均數將會是微不足道的。 –
@EdwinBuck 好的我做了你所說的但仍然短小,所以如果你可能看看我的新代碼,並讓我知道你的想法。 – Paragon