有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java多数组输入

我无法创建循环以向多阵列添加小时数。当我使用这个方法时,有一个错误。我怀疑for循环没有捕获输入

import javax.swing.JOptionPane;

public class Gain 
{
    // Defining array names.
    String[] name = {"A", “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “L”}; 
    int[][] hours = new int[10][3];

    public final int Hours() 
    {
    boolean canceled = false;
    for (int index = 0; index < name.length; index++) {
        JOptionPane.shoeMessageDialog(“Please enter " + name[index] + “’s hours”);
            for (int x = 0; x <= hours.length; x++)
               for (int y = 0; y <= hours[x].length; y++)
        Integer value = promptForInt(artist[index] + “’s first hour: ”);
        if (value != null) {
          while (value < 0)
        {
            JOptionPane.showMessageDialog(null, "Please positive figures." + "\nPlease try again.");
            value = promptForInt(name[index] + “’s first hour: ”);
        }
            pay[x][y] = value;
        } else {
            canceled = true;
            break;
        }
    }

    public static void main(String[] args) // Main program
    {
        for (int x = 0; x <= name.length; x++)
           for (int y = 0; y <= hours.length; y++)
              System.out.println(x, y);
    }

共 (1) 个答案

  1. # 1 楼答案

    尝试在括号中添加如下内容:

    import javax.swing.JOptionPane;
    
    public class Gain 
    {
        // Defining array names.
        String[] name = {"A", “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “L”}; 
        int[][] hours = new int[10][3];
    
        public final int Hours() 
        {
            boolean canceled = false;
            for (int index = 0; index < name.length; index++)
            {
                JOptionPane.shoeMessageDialog("Please enter " + name[index] + "’s hours");
                for (int x = 0; x <= hours.length; x++)
                {
                    for (int y = 0; y <= hours[x].length; y++)
                    {
                        Integer value = promptForInt(artist[index] + “’s first hour: ”);
                        if (value != null)
                        {
                            while (value < 0)
                            {
                                JOptionPane.showMessageDialog(null, "Please positive figures." + "\nPlease try again.");
                                value = promptForInt(name[index] + “’s first hour: ”);
                            }
                            pay[x][y] = value;
                        } 
                        else
                        {
                            canceled = true;
                            break;
                        }
                    }
                }
    
            }
        }
    
        public static void main(String[] args) // Main program
        {
            for (int x = 0; x <= name.length; x++)
               for (int y = 0; y <= hours.length; y++)
                  System.out.println(x, y);
        }
    

    虽然当后面只有一行代码时(如在main方法中),可以避免for循环中使用括号,但当有整个块时,需要使用括号,就像在Hours()方法中一样。就我个人而言,我喜欢一直使用括号,因为它使代码更具可读性,但这只是我的想法。:)