我正在编写一个程序来打印保龄球比赛的比分。分数在称为 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)需要作为字符串打印出来,例如“X”代表值 10。我是试图将整数转换为字符串,但不知道如何修复发生的错误,“无法取消引用整数。”我没有太多编程经验,所以在这一点上直接的答案对我最有帮助。
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("+---+---+---+");
}
最佳答案
int
是原始类型,因此它没有任何处理方法。尝试
1. String.valueOf(rolls[i])
2. Integer.toString(rolls[i])
3. rolls[i] + ""
4. new Integer(rolls[i]).toString()
代替
rolls[i].toString()
关于java - int 不能被取消引用,将 int 值从数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36505815/