【问题标题】:int cannot be dereferenced, convert an int value from an array to a stringint 不能被取消引用,将 int 值从数组转换为字符串
【发布时间】: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 包或什么。

标签: java arrays int tostring


【解决方案1】:

int 是一个原始类型,所以它没有处理方法。试试

1. String.valueOf(rolls[i])
2. Integer.toString(rolls[i])
3. rolls[i] + ""
4. new Integer(rolls[i]).toString()

而不是

rolls[i].toString()

【讨论】:

  • 我不鼓励使用 #3 和 #4。
  • 成功了!我假设 String.valueOf(rolls[i]);是在 i 处获取数组的值,然后将其转换为字符串,对吗?
  • @Liz String.valueOf() 已超载。在这种情况下,它使用String.valueOf(int),它将int 值转换为String。澄清一下,数组查找不是由valueOf() 完成的,而是由表达式rolls[i] 完成的,然后它将单个值传递给valueOf() 方法。
  • @Andreas,是的,我也是,但 4 路看起来并不好。 OP 应该知道所有这些方法
  • @AndrewTobilko 当然,我赞成你列出所有方式,但并非所有方式都是平等的,指导新手程序员同样是引导他们远离邪恶?。 ;-)
【解决方案2】:

当您调用 rolls[i].toString() 时,您实际上是在 int 上调用 .toString()。由于int 上不存在该方法,因此会引发错误。

可能的解决方案是将int 直接传递给您的打印方法。由于这些是整数,因此转换为 String 不会对其进行任何不同的格式化。另一种解决方案是使用String.valueOf(rolls[i]) 将您的int 转换为String

【讨论】:

    猜你喜欢
    • 2012-03-24
    • 2015-05-21
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多