无法取消引用int,请将int值从数组转换为字符串 [英] int cannot be dereferenced, convert an int value from an array to a string
问题描述
我正在编写一个程序来打印保龄球比赛的分数.分数以名为rolls的int数组提供:
I am writing a program to print out the scores of a bowling game. The scores are provided in an int array called rolls:
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".我是试图将int转换为字符串,并且不知道如何解决发生的错误,即无法取消引用int".我没有太多的编程经验,因此简单的答案现在对我最有帮助.
I am converting the score values to strings in order to print them out because some of the values, like 10 which represents a strike, need to be printed out as strings, like "X" for a value of 10. I am trying to convert the ints to strings and do not know how to fix the error thats occurring, "int cannot be dereferenced." I don't have a lot of experience programming so straightforward answers will be most helpful to me at this point.
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
是原始类型,因此没有可使用的方法.试试
int
is a primitive type, so it has no methods for working with. Try
1. String.valueOf(rolls[i])
2. Integer.toString(rolls[i])
3. rolls[i] + ""
4. new Integer(rolls[i]).toString()
代替
rolls[i].toString()
这篇关于无法取消引用int,请将int值从数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!