0

希望有人可以帮助我,我是 Java 的菜鸟。我的问题是,当我打印数组时,我得到了一些字符,我想打印数组中的点数。(请忽略不相关的代码,我什至还没有完成)

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class MainClass {

        public static void main (String[] args){


        int arrayIntC, arrayIntE, nOC, nOE;
        char confirmNumbers;

        do{ 
            String numberOfCharacters = JOptionPane.showInputDialog("Enter the number of characters:");
            String numberOfEnemies = JOptionPane.showInputDialog("Enter the number of enemies:");


            nOC = Integer.parseInt(numberOfCharacters);
            nOE = Integer.parseInt(numberOfEnemies);


            JOptionPane.showMessageDialog(null, "Number of characters: " + nOC, "DnD Organizer", JOptionPane.PLAIN_MESSAGE);
            JOptionPane.showMessageDialog(null, "Number of enemies: " + nOE, "DnD Organizer", JOptionPane.PLAIN_MESSAGE);


            confirmNumbers = JOptionPane.showInputDialog("Are the numbers correct? (y/n)").charAt(0);


        }while(confirmNumbers != 'y');

        JOptionPane.showMessageDialog(null, "Banarne", "DnD Organizer", JOptionPane.PLAIN_MESSAGE);

        arrayIntC = nOC -1;
        arrayIntE = nOC -1;

        int[] arrayC = {arrayIntC}, arrayE = {arrayIntE};

        JOptionPane.showMessageDialog(null, "Platser i arrayC: ", "DnD Organizer", JOptionPane.PLAIN_MESSAGE);

        /*for (int i = 0; i <arrayC.length; i++){

            System.out.printf("%5d%8d", i, arrayC[i]);


        }*/

        JOptionPane.showMessageDialog( null, arrayC );


    System.exit(0);
    }//main

}//MainClass
4

1 回答 1

0

似乎您在这里遇到了一些不同的问题。您对以下代码的意图是什么?

int[] arrayC = {arrayIntC}, arrayE = {arrayIntE};

如果用户输入“10”作为字符数,arrayC将被设置为一个大小为 1 的数组,其中包含一个值为 10 的元素。

如果您的意图是创建一个大小为 10 的空数组,则需要像这样初始化数组:

int[] arrayC = new int[arrayIntC];

如果要打印数组的大小,可以将代码更改为如下所示:

JOptionPane.showMessageDialog( null, arrayC.length );
于 2013-09-05T13:47:28.077 回答