【发布时间】:2016-07-30 02:01:04
【问题描述】:
我正在编写一个程序来打印保龄球比赛的分数。分数在一个名为 rolls 的 int 数组中提供:
rolls = new int[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 4, 6, 3, 2, 7, 1, 8, 0, 9, 0, 0};
我将分数值转换为字符串以便打印出来,因为某些值,例如代表罢工的 10,需要打印为字符串,例如值 10 的“X”。我是试图将整数转换为字符串并且不知道如何修复发生的错误,“无法取消引用整数”。我没有很多编程经验,所以在这一点上,直截了当的答案对我最有帮助。
public class Bowling {
private int frames;
private int[] rolls;
int currentFrameCount=0;
int currentFrame=1;
int grantTotal=0;
int ballCount=1;
public Bowling(int[] rolls, int frames)
{
this.frames=frames;
this.rolls=rolls;
}
public void play()
{
String box1="";
String box2="";
String box3="";
int totalScore = 0;
for(int i=0; i < rolls.length; i += 2)
{
totalScore += rolls[i] + rolls[i+1];
if(rolls.length % 2 == 0)
{
printNormalFrame(rolls[i].toString(), rolls[i+1].toString(), *totalScore.toString());***-->having the error here**
}
else
{
printBiggerFrame(rolls[i].toString(), rolls[i+1].toString(), totalScore.toString());
}
}
}
public void printNormalFrame(String box1, String box2, String totalScore)
{
System.out.println("+---+---+");
System.out.println("| " + box1 + " | " + box2 + " |");
System.out.println("|---+---|");
System.out.println("| " + totalScore + " |"); //todo do the padding correctly using String.Format(...),
System.out.println("+---+---+");
}
public void printBiggerFrame(String box1, String box2, String box3, int totalScore)
{
{
System.out.println("+---+---+---+");
System.out.println("| " + box1 + " | " + box2 + " | " + box3 + " |");
System.out.println("|---+---+---|");
System.out.println("| "+totalScore+"|"); //todo the padding correctly using String.Format(...)
System.out.println("+---+---+---+");
}
【问题讨论】:
-
你觉得
totalScore.toString()应该怎么做?你为什么这么认为?当你查看编译器给你的错误信息时,你发现了什么? -
我认为使用 totalScore.toString() 会获取我的 totalScore 的 int 值并将其转换为字符串,以便我可以以我想要的方式打印出来。我查找了错误并找到了关于使用 toString 将 int 转换为字符串的 cmets,这是我尝试过的,以及关于与 "" 连接的一些我不明白的东西。我不知道我是否应该尝试这样的事情: System.out.println(Arrays.toString(array));通过导入 util.Arrays 包或什么。