简单的数组类工作但输出错误

时间:2015-11-29 05:11:06

标签: java arrays

我正在做一些Java代码挑战,并试图用构造函数和main()而不是常规方法编写整个类,只是为了进行一些额外的练习。我已经尝试过运行它在Eclipse和JGrasp中得到了相同的输出:[I @ 6d06d69c

有没有人知道我做错了什么?在此先感谢您的帮助:)

board = [10,10,10,10,10,10,10,10,10,10,
         10,10,10,10,10,10,10,10,10,10,
         10,10,10,10,10,10,10,10,10,10,
         10,10,20,20,20,20,20,20,10,10,
         10,10,20,20,20,20,20,20,10,10,
         10,10,20,20,20,20,20,20,10,10,
         20,20,30,30,40,40,30,30,20,20,
         20,30,30,40,50,50,40,30,30,20,
         30,40,50,50,50,50,50,50,40,30,]

def show():
    print(board [0], board[1], board[2], board[3], board[4], board[5], board[6], board[7], board[8], board[9])
    print(board [10], board[11], board[12], board[13], board[14], board[15], board[16], board[17], board[18], board[19])
    print(board [20], board[21], board[22], board[23], board[24], board[25], board[26], board[27], board[28], board[29])
    print(board [30], board[31], board[32], board[33], board[34], board[35], board[36], board[37], board[38], board[39])
    print(board [40], board[41], board[42], board[43], board[44], board[45], board[46], board[47], board[48], board[49])
    print(board [50], board[51], board[52], board[53], board[54], board[55], board[56], board[57], board[58], board[59])
    print(board [60], board[61], board[62], board[63], board[64], board[65], board[66], board[67], board[68], board[69])  
    print(board [70], board[71], board[72], board[73], board[74], board[75], board[76], board[77], board[78], board[79])
    print(board [80], board[81], board[82], board[83], board[84], board[85], board[86], board[87], board[88], board[89])


while True:
    seat = int(input("Please enter a seat or price"))

    if board[seat] != "0" :
       board[seat] =  "0"
    else:
        print("This seat is laready sold")


    show()

2 个答案:

答案 0 :(得分:2)

result是一个数组,您无法使用System.out.println直接打印它。而是尝试以下。

System.out.println(Arrays.toString(result));

如果想要更多受控的数组元素输出,可以遍历数组中的元素并逐个打印。您也可以在toString课程中查看Object方法的javadoc,了解[I@6d06d69c的含义。

答案 1 :(得分:0)

您无法通过java中的System.out.println()方法直接打印数组。 为此,您必须使用util包的Array类中提供的toString()方法。此外,您可以直接使用结果[7]查看值。

import java.util.*;
class MakeLast {
    public MakeLast(int[]nums){
        int[] result = new int[nums.length *2];
        result[result.length-1] = nums[nums.length-1];
    System.out.println(result.length);
        System.out.println(result[7]);
    System.out.println(Arrays.toString(result));
    }

    public static void main(String[] args) {
    int[] nums = {3, 5, 35, 23};
    new MakeLast(nums);

    }
}