我创建了一个程序,从美式足球得分,并产生所有可能的方法来获得美式足球的得分。我刚刚创建了一个无转换的触地得分方法(将得分值递减6),该方法结合了一个字段目标方法(将值递减3)和一个安全性(将值递减2)。问题是,当键入偶数值时,没有包含达阵,字段目标和安全性的适当组合。该程序适用于奇数值。我怀疑问题可能是 fieldgoalsnumbersprinted 返回其String值。
static int sixtouchdownnumbers(int c) //return type doesn't matter
{
int touchdowns6 = c;
int touch6counter = 0;
int touch6divided = touchdowns6/6;
int lesserdowns6 = touch6divided -1;
String booth;
String box;
String scoreboard;
String boothbox;
String seat;
String boothboxseat;
if(touchdowns6 < 6)
{
return touchdowns6; //this value doesn't matter. I just have this here so the method stops if the score is less than 6.
}
touchdowns6 = c;
touch6counter = 0;
for(int i = 0; i <= lesserdowns6; i++)
{
touchdowns6 = touchdowns6-6;
touch6counter++;
booth = "there were " + touch6counter + " touchdowns without conversion";
box = Safetynumbersprinted(touchdowns6);
seat = Fieldgoalnumbersprinted(touchdowns6);
boothbox = booth+box;
boothboxseat = boothbox+seat;
if(boothboxseat.contains("safeties")) //fixes an issue with field goal loop generating bad scores.
System.out.println(boothboxseat);
}
return 0;
}
static String Fieldgoalnumbersprinted(int d)
{
int fieldgoals = d;
int goalcounter = 0;
int fielddivided = fieldgoals/3;
int lessergoals = fielddivided -1;
String scoreb;
String oard;
String scoreboard;
String nosebleed = "Error"; //if this is printed, the for loop didn't assign the right value.
if(fieldgoals < 3)
{
return "";
}
fieldgoals = d;
goalcounter = 0;
for(int i = 0; i <= lessergoals; i++)
{
fieldgoals = fieldgoals-3;
goalcounter++;
scoreb = ", " + goalcounter + " field goals";
oard = Safetynumbersprinted(fieldgoals);
scoreboard = scoreb+oard;
if(scoreboard.contains("safeties")) //fixes an issue with field goal loop generating bad scores.
nosebleed = scoreboard; //potential cause of the problem.
}
return nosebleed;
}
static String Safetynumbersprinted(int a)
{
int safeties = a;
int safetycounter = 0;
int safedivided;
String scorehalf;
if ( (a & 1) == 0 ) //only even scores can be reduced to zero.
{
safedivided = a/2;
for(int i = 0; i < safedivided; i++)
{
safeties = safeties-2;
safetycounter++;
}
scorehalf = " and " + safetycounter + " safeties: ";
return scorehalf;
}
else
{
return "";
}
}