Java预测错误随机不起作用?

时间:2017-10-03 21:27:08

标签: java for-loop random

您好我一直致力于预测数字的程序。我现在已经多次查看了代码并对其进行了编辑。出于某种原因,teamOneScored始终高于teamTwoScored。这对我没有任何意义。

发生的错误是什么?

这是我的CompareEngine类:

 package compare;

 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.Random;

 import data.Main;

 public class CompareEngine 
 {
     public static void Comparison(int firstTeamTotal, int secondTeamTotal, 
     int[] firstPositionAmount, int[] secondPositionAmount, int[] 
      firstPosition, int[] secondPosition)
 {
    System.out.println("             Comparisons:");
    System.out.println("----------------------------------------");
    System.out.println(Main.firstTeam + "           vs.           " + Main.secondTeam);
    System.out.println("----------------------------------------");
    System.out.println(firstTeamTotal + "             Total " + "               " + secondTeamTotal);
    System.out.println("");
    System.out.println(firstPosition[0]-1+"-"+firstPosition[1]+"-"+firstPosition[2] + "         Formation            " + (secondPosition[0]-1) +"-"+secondPosition[1]+"-"+secondPosition[2]);
    System.out.println("");
    System.out.println(firstPosition[0] + "             Defenders " + "               " + secondPosition[0]);
    System.out.println(firstPositionAmount[0] + "         Defense Total " + "           " + secondPositionAmount[0]);
    System.out.println("");
    System.out.println(firstPosition[1] + "            Midfielders " + "              " + secondPosition[1]);
    System.out.println(firstPositionAmount[1] + "         Midfield Total " + "          " + secondPositionAmount[1]);
    System.out.println("");
    System.out.println(firstPosition[2] + "              Attackers " + "              " + secondPosition[2]);
    System.out.println(firstPositionAmount[2] + "           Attack Total " + "           " + secondPositionAmount[2]);      
}

public static void RunGame(int firstTeamTotal, int secondTeamTotal, int[] firstPositionAmount, 
        int[] secondPositionAmount, int[] firstPosition, int[] secondPosition) throws IOException
{
    int depth;

    System.out.println("What depth do you want to run?");
    depth = Main.read.nextInt();

    int firstShotCount = 0;
    int secondShotCount = 0;

    int firstDefense = firstPositionAmount[0] + firstPositionAmount[1]/2;
    int secondDefense = secondPositionAmount[0] + secondPositionAmount[1]/2;

    int firstAttack = firstPositionAmount[2] + firstPositionAmount[1]/2;
    int secondAttack = secondPositionAmount[2] + secondPositionAmount[2]/2;


    while(firstAttack*3 > secondDefense)
    {
        firstShotCount = firstShotCount + 1;
        firstAttack = firstAttack - 5;
    }

    while(secondAttack*3 > firstDefense)
    {

        secondShotCount = secondShotCount + 1;
        secondAttack = secondAttack - 5;
    }

    System.out.println(firstShotCount);
    System.out.println(secondShotCount);

    PossessionControl(Main.firstTeam, Main.secondTeam);

    int[] teamOneScored = new int[99];
    int[] teamTwoScored = new int[99];
    int[] oneShotOn = new int[99];
    int[] twoShotOn = new int[99];
    int[] OnePossession = new int[99];
    int[] TwoPossession = new int[99];

    Random random = new Random();

    for(int i = 0; i < depth; i++)
    {
        for(int x = 0; firstShotCount >= x; x++)
        {
            int shot = random.nextInt(10 - 1 + 1) + 1;

            if (shot > 8)
            {
                teamOneScored[i] = teamOneScored[i] + 1;
            }

            if (shot > 4)
            {
                oneShotOn[i] = oneShotOn[i] + 1;
            }
        }

        for(int y = 0; secondShotCount >= y; y++)
        {
            int shot = random.nextInt(10 - 1 + 1) + 1;

            if (shot > 8)
            {
                teamTwoScored[i] = teamTwoScored[i] + 1;
            }

            if (shot > 4)
            {
                twoShotOn[i] = twoShotOn[i] + 1;
            }
        }
        System.out.println(teamOneScored[i] + ":" + teamTwoScored[i]);
    }
}

static File firstDataFile = new File("src/playerdata/" + Main.firstTeam);
static File secondDataFile = new File("src/playerdata/" + Main.secondTeam);

static int teamOnePossessionTotal = 0;
static int teamTwoPossessionTotal = 0;

public static void PossessionControl(String firstTeam, String secondTeam) throws IOException
{       

    BufferedReader br1 = new BufferedReader(new FileReader(firstDataFile));
    String line = "";
    for(int i = 1; i+1 < 13; i++) 
    {
        line = br1.readLine();
        teamOnePossessionTotal = teamOnePossessionTotal + PossessionStatTotal(line);
    }
    br1.close();

    BufferedReader br2 = new BufferedReader(new FileReader(secondDataFile));

    for(int i = 1; i+1 < 13; i++) 
    {
        line = br2.readLine();
        teamTwoPossessionTotal = teamTwoPossessionTotal + PossessionStatTotal(line);
    }
    br2.close();
}

public static int PossessionStatTotal(String line)
{
    int value = 0;
    String[] stats = line.split("-");
    String position = stats[1];

    if(!(position.equals("GK")))
    {       
        String passing = stats[5];
        String positioning = stats[7];
        String ballControl = stats[9];
        value = (int) ((int) (Integer.valueOf(passing)*.5) + Integer.valueOf(positioning)*.2 + Integer.valueOf(ballControl)*.1);
    }

    return value;
}   
}

这是我的主要课程:

 package data;


 import java.io.IOException;
 import java.util.Scanner;

 public class Main 
 {
public static String currentTeam;

public static void main(String[] args) throws IOException 
{
    compareTeams();
    //setOveralls();
}

public static void setOveralls() throws IOException
{
    Scanner read = new Scanner(System.in);

    System.out.println("Which team do you like?");
    currentTeam = read.nextLine();
    read.close();

    PlayerOverall.eraseOverallFile();
    PlayerOverall.getPlayerInfo(13, currentTeam);

}

public static String firstTeam;
public static String secondTeam;
public static Scanner read = new Scanner(System.in);


public static void compareTeams() throws IOException
{
    System.out.println("What is the first team?");
    firstTeam = read.nextLine();

    System.out.println("What is the second team?");
    secondTeam = read.nextLine();

    compare.CompareTeams.compare(firstTeam, secondTeam);
}

}

这是PlayerOverall Class,这只是决定了他们的整体。

 package data;

 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;


 public class PlayerOverall 
 {
//NOTE TO SELF -- CHANGE OUT OF HARD CODE   VVVVVVVVVVV
static File dataFile = new File("src/playerdata/" + Main.currentTeam);
static File overallFile = new File("src/overalls/" + Main.currentTeam);


public static String getPlayerInfo(int numLine, String currentTeam) throws IOException 
{
    BufferedReader br = new BufferedReader(new FileReader(dataFile));
    String line = "";
    for(int i = 1; i+1 < numLine; i++) 
    {
        line = br.readLine();
        evaluatePlayer(line);
    }
    br.close();
    return line; 
}

public static void evaluatePlayer(String line) throws IOException
{ 
    String[] stats = line.split("-");
    String position = stats[1];

    int overall = 0;

    if(position.equals("GK"))
    {
        int height = Integer.parseInt(stats[2]);
        int diving = Integer.parseInt(stats[3]);
        int catching = Integer.parseInt(stats[4]);
        int shooting = Integer.parseInt(stats[5]);
        int reflexes = Integer.parseInt(stats[6]);
        int positioning = Integer.parseInt(stats[7]);

        overall = (int) ((int) ((int) ((int) ((int) ((int) (height*.05) + diving*.2) + catching*.2) + shooting*.1) + reflexes*.2) + positioning*.25); 
        setOverall(position, overall);
        //OVERALL WORKS CORRECT NOW DO THE ADDITION OF OVERALL TO THE FILE!!
    }
    else
    {
        int speed = Integer.parseInt(stats[2]);
        int acceleration = Integer.parseInt(stats[3]);
        int distance = Integer.parseInt(stats[4]);
        int passing = Integer.parseInt(stats[5]);
        int shooting = Integer.parseInt(stats[6]);
        int positioning = Integer.parseInt(stats[7]);
        int defending = Integer.parseInt(stats[8]);
        int ballControll = Integer.parseInt(stats[9]);

        if(position.equals("LB") || position.equals("RB"))
        {
            overall = (int) ((int) ((int) ((int) ((int) ((int) ((int) ((int) (speed*.2) + acceleration*.2) + distance*.1) + passing*.1) + shooting*.1) + positioning*.15) + defending*.15) + ballControll*.1); 
        }
        else if(position.equals("CB"))
        {
            overall = (int) ((int) ((int) ((int) ((int) ((int) ((int) ((int) (speed*.095) + acceleration*.1) + distance*.1) + passing*.1) + shooting*.05) + positioning*.2) + defending*.3) + ballControll*.1); 
        }
        else if(position.equals("CM"))
        {
            overall = (int) ((int) ((int) ((int) ((int) ((int) ((int) ((int) (speed*.05) + acceleration*.05) + distance*.15) + passing*.2) + shooting*.1) + positioning*.2) + defending*.1) + ballControll*.1) + 10; 
        }
        else if(position.equals("LM") || position.equals("RM"))
        {
            overall = (int) ((int) ((int) ((int) ((int) ((int) ((int) ((int) (speed*.2) + acceleration*.2) + distance*.1) + passing*.1) + shooting*.133) + positioning*.1) + defending*.033) + ballControll*.133) + 5; 
        }
        else if(position.equals("ST"))
        {
            int stength = Integer.parseInt(stats[10]);
            overall = (int) ((int) ((int) ((int) ((int) ((int) ((int) ((int) ((int) (speed*.133) + acceleration*.133) + distance*.033) + passing*.05) + shooting*.225) + positioning*.225) + defending*.0) + ballControll*.1) + stength*.1) + 5; 
        }

        setOverall(position, overall);
    }   
}

public static void setOverall(String position, int overall) throws IOException
{
    try (FileWriter fw = new FileWriter(overallFile, true)) 
    {

        fw.append(position + "-" + overall +"\n");
    }

}

public static void eraseOverallFile() throws IOException
{
    FileWriter fw = new FileWriter(overallFile);
    fw.write("");
    fw.close();
}
}

我要获取的文件只是常规文件。它们看起来像这样:

 Lukas Hradecky-GK-63-81-81-74-89-82
 Jetro Willems-LB-83-86-80-79-76-72-76-81
 David Abraham-CB-76-83-68-74-70-76-80-61
 Simon Falette-CB-60-71-77-57-50-74-84-56
 Timmy Chandler-RB-80-76-83-74-68-71-77-74
 Marco Fabian-CM-70-76-75-78-80-77-36-85
 Jonathan De Guzman-CM-78-79-70-78-80-78-51-83
 Mijat Gacinovic-RM-78-78-74-70-68-69-61-81
 Ante Rebic-LM-76-79-71-62-78-72-40-75
 Sebastien Haller-ST-75-79-73-64-77-77-35-79-91
 Kevin Prince Boateng-ST-74-74-67-79-82-78-70-83-83

您可以随意命名此文件。在适当的位置创建另一个文件,它将生成所需的一切。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为你错过了[mcve]中的 minimal

TL; DR ......但发现:

int firstDefense = firstPositionAmount[0] + firstPositionAmount[1]/2;
int secondDefense = secondPositionAmount[0] + secondPositionAmount[1]/2;

int firstAttack = firstPositionAmount[2] + firstPositionAmount[1]/2;
int secondAttack = secondPositionAmount[2] + secondPositionAmount[2]/2;

指数是:

0 1
0 1
2 1
2 2

这似乎不一致。也许最后应该是1?