【问题标题】:How can I make my code go from one void to another in my menu code and get it to randomly generate inputs?如何让我的代码在菜单代码中从一个空白变为另一个空白并让它随机生成输入?
【发布时间】:2020-05-01 02:46:45
【问题描述】:

我的菜单代码(主)和 testMatrix 代码不能正常工作,菜单代码只是不正确地转到 GetMatrix 方法(它让用户输入一个 4x4 矩阵而不是所需的 3X4 并且 testMatrix 代码甚至没有工作,因为它只是使用用户输入法。程序要求我更改许多程序头,以使代码甚至可以运行而不会崩溃。

package sumelementsbycolumn;

import java.util.Random;
import java.util.Scanner;

public class SumElementsByColumn 
{
        private final static Scanner myScan = new Scanner(System.in);

    //========================= void main() ==============================
    public static void main(String[] args) 
    {
        char choice;
        do
        {
            final int ROWS = 3;
            final int COLUMNS = 4;
            double[][] matrix = new double[ROWS][COLUMNS];
            choice = getMenu();
            switch (choice) 
            {
                case '1': 
                {
                    matrix = GetMatrix(matrix);
                    sumColumn(matrix);
                    sumPrint(matrix);
                    break;
                }
                case '2': 
                {
                    matrix = testMatrix(matrix);
                    sumColumn(matrix);
                    sumPrint(matrix);
                    break;
                }
                case '0':
                    System.out.println("\n==================================");
                    System.out.println("\n=    Thank You for Using The     ="
                                     + "\nSum Elements By Column Determiner");
                    System.out.println("\n=            Goodbye!            =");
                    System.out.println("\n==================================");
            }
        }while(choice != '0');

    }

    //========================= char GetMenu() ===========================
    private static char getMenu() 
    {
        System.out.print("===================================\n"
                + "=Sum Elements By Column Determiner=\n"
                + "===================================\n\n"
                + "\t(1) Input 3 rows of integers\n"
                + "\t(2) Test system using random numbers\n"
                + "\t(0) Exit the program\n"
                + "Choose: ");
        char choice = myScan.next().toUpperCase().charAt(0);

        return choice;

    }
    //========================= void sumPrint() ==============================
    private static void sumPrint(double[][] theMatrix)
        {

        // Read a 3-by-4 matrix
        final int ROWS = 3;
                final int COLUMNS = 4;
                double[][] matrix = new double[ROWS][COLUMNS];
                GetMatrix(matrix);

        // Display the sum of each column
        for (int col = 0; col < matrix[0].length; col++) 
                {   
            System.out.println(
                "Sum of the elements at column " + col + 
                " is " + sumColumn(matrix));
        }
    }
    //========================= double getMatrix() ===========================
    private static double[][] GetMatrix(double[][] matrix)
        {
            final int ROWS = 3;
            final int COLUMNS = 4;
            double[][] m = new double[ROWS][COLUMNS];

        // Prompt the user to enter a 3-by-4 matrix
            System.out.println("Enter a " + ROWS + "-by-" + 
            COLUMNS + " matrix row by row:");
            for (int row = 0; row < m.length; row++)
                for (int col = 0; col < m[row].length; col++) 
                    m[row][col] = myScan.nextDouble();
                    while(!myScan.hasNextDouble())  
                    {
                        System.out.println("That is not a valid number!");
                        System.out.println("Re-Enter the Matrix Values: ");
                        myScan.next();
                    }
            return m;
    }

    //========================= double sumColumn() ===========================
    public static double sumColumn(double[][] m)
        {
        double sum = 0;
                int columnIndex = 0;
                for (double[] m1 : m) 
                {
                sum += m1[columnIndex];
                }
        return sum;
    }
    //========================= double testMatrix() ===========================
    private static double[][] testMatrix(double[][] blankMatrix)
    {
        Random rnd = new Random();


        int count = 0;

            for (double[] blankMatrix1 : blankMatrix) 
            {
                for (int col = 0; col < blankMatrix.length; col++) 
                {
                    {
                        blankMatrix1[col] = rnd.nextInt(100);
                    }
                }
            }
        return blankMatrix;
    }

}

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

    我稍微调整了代码。这应该可以工作。问题在于 sumColumn 方法的列索引始终设置为 0。

    import java.util.Random;
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class SumElementsByColumn
    {
        private final static Scanner myScan = new Scanner(System.in);
    
        //========================= void main() ==============================
        public static void main(String[] args)
        {
            char choice;
            do
            {
                final int ROWS = 3;
                final int COLUMNS = 4;
                double[][] matrix = new double[ROWS][COLUMNS];
                choice = getMenu();
                switch (choice)
                {
                    case '1':
                    {
                        matrix = GetMatrix(matrix);
                       // sumColumn(matrix);
                        sumPrint(matrix);
                        break;
                    }
                    case '2':
                    {
                        matrix = testMatrix(matrix);
                       // sumColumn(matrix);
                        sumPrint(matrix);
                        break;
                    }
                    case '0':
                        System.out.println("\n==================================");
                        System.out.println("\n=    Thank You for Using The     ="
                                + "\nSum Elements By Column Determiner");
                        System.out.println("\n=            Goodbye!            =");
                        System.out.println("\n==================================");
                }
            }while(choice != '0');
    
        }
    
        //========================= char GetMenu() ===========================
        private static char getMenu()
        {
            System.out.print("===================================\n"
                    + "=Sum Elements By Column Determiner=\n"
                    + "===================================\n\n"
                    + "\t(1) Input 3 rows of integers\n"
                    + "\t(2) Test system using random numbers\n"
                    + "\t(0) Exit the program\n"
                    + "Choose: ");
            char choice = myScan.next().toUpperCase().charAt(0);
    
            return choice;
    
        }
        //========================= void sumPrint() ==============================
        private static void sumPrint(double[][] theMatrix)
        {
    
            // Read a 3-by-4 matrix
            final int ROWS = 3;
            final int COLUMNS = 4;
           // double[][] matrix = new double[ROWS][COLUMNS];
            //GetMatrix(matrix);
    
            // Display the sum of each column
            for (int col = 0; col < theMatrix[0].length; col++)
            {
                System.out.println(
                        "Sum of the elements at column " + col +
                                " is " + sumColumn(theMatrix,col));
            }
        }
        //========================= double getMatrix() ===========================
        private static double[][] GetMatrix(double[][] matrix)
        {
            final int ROWS = 3;
            final int COLUMNS = 4;
            double[][] m = new double[ROWS][COLUMNS];
    
            // Prompt the user to enter a 3-by-4 matrix
            System.out.println("Enter a " + ROWS + "-by-" +
                    COLUMNS + " matrix row by row:");
            for (int row = 0; row < m.length; row++) {
                for (int col = 0; col < m[row].length; col++) {
                    m[row][col] = myScan.nextDouble();
                }
            }
            while(!myScan.hasNextDouble())
            {
                System.out.println("That is not a valid number!");
                System.out.println("Re-Enter the Matrix Values: ");
                myScan.next();
            }
            return m;
        }
    
        //========================= double sumColumn() ===========================
        public static double sumColumn(double[][] m,Integer col)
        {
            double sum = 0;
            int columnIndex = 0;
    
           // System.out.println(Arrays.deepToString(m));
            for (double[] m1 : m)
            {
               // System.out.println(" sumcolumn "+m1[col]);
                sum += m1[col];
            }
            return sum;
        }
        //========================= double testMatrix() ===========================
        private static double[][] testMatrix(double[][] blankMatrix)
        {
            Random rnd = new Random();
    
    
            int count = 0;
    
            for (double[] blankMatrix1 : blankMatrix)
            {
                for (int col = 0; col < blankMatrix.length; col++)
                {
                    {
                        blankMatrix1[col] = rnd.nextInt(100);
                    }
                }
            }
            return blankMatrix;
        }
    
    }
    

    用于测试的输入数组:

    ===================================
    =Sum Elements By Column Determiner=
    ===================================
    
        (1) Input 3 rows of integers
        (2) Test system using random numbers
        (0) Exit the program
    Choose: 1
    Enter a 3-by-4 matrix row by row:
    1 2 3 4
    5 6 7 8
    9 10 11 12
    

    输出:

    Sum of the elements at column 0 is 15.0
    Sum of the elements at column 1 is 18.0
    Sum of the elements at column 2 is 21.0
    Sum of the elements at column 3 is 24.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 2020-02-07
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多