我有一个功能齐全的程序,我需要分成3种方法

时间:2016-03-30 03:56:32

标签: java arrays methods

所以我对java代码和编程很好,只要它只使用main方法。但是使用多种方法对我来说很困惑。但是为了使它更专业,它显然需要分开,以便更容易更改。我一直在寻找一个类似于我的程序的例子,但我找不到一个,现在我只是卡住了。

功能齐全的计划

package test5555;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class Test5555 {

    private static int[] randomInteger;

    public static void main(String[] args) {

        boolean validInput = false;
        randomInteger = new int[101];
        Random rand = new Random();
        for (int i = 0; i < randomInteger.length; i++)
            randomInteger[i] = rand.nextInt();
        int indexPosition = 0;

        Scanner input = new Scanner(System.in); 
            System.out.println("Please enter an integer for the array index position: ");

            String s = null;
            while (!validInput) {
                try {
                    if (s != null) {
                        indexPosition = Integer.parseInt(s);
                    } else {
                        indexPosition = input.nextInt();
                    }
                    System.out.println(randomInteger[indexPosition]);
                    validInput = true;
                } catch (InputMismatchException | NumberFormatException | IndexOutOfBoundsException ex) {

                    System.out.println("Please enter a valid integer between 0 and 100 or type quit to exit: ");
                    input.nextLine();
                    s = input.next();
                    if (s.equals("quit")) {
                        System.exit(0);
                    }
                }
            }
        }
    }

我正在尝试执行以下操作:

  • 调用创建具有100个随机生成的整数的数组的方法
  • 调用一个方法,提示用户输入数组的索引位置并返回输入的值。如果用户输入的值不是有效整数,则抛出“输入不匹配异常”,提示用户出现问题并允许他们输入正确的整数或退出程序
  • 调用一个方法,该方法接收用户输入的值作为数组中的索引位置。该方法将在输入值的索引位置显示相应的值。如果用户输入的值超出数组中的有效索引位置,则抛出“数组索引超出边界异常”。
  • 应在相关方法中检测所有异常,并在主方法
  • 中处理

到目前为止,我曾多次尝试和改变并失败了

package exceptionhandling;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import java.util.*;

public class ExceptionHandling {
    private static int[] randomInteger;

    public static void main(String[] args) {

        //Need to call the three methods
        //Need to handle all exceptions

    }

    public static void createArray() //Method to create the array filling with 100 random integers
        {

            randomInteger = new int[101];
            Random rand = new Random();
            for (int i = 0; i < randomInteger.length; i++)
                randomInteger[i] = rand.nextInt();

        }

    public static void enterIndex(int[] numbers) //Is this calling the array correctly? Method to prompt the user to enter indexPosition
        {
            //should I just put my 'try else catch' loop in here along with the input InputMismatchException and have that take the value of the indexPosition variable which I will call in the main method?

        }

    public static void getarrayValue(int[] numbers) //Definitely not formatted properly. Method to get the value from the array based on the indexPosition from user

    // Don't think this method should be void, it should return the value from the array associated with the indexPosition. Do I do the same loop as above but this time with the IndexOutOfBoundsException and then return System.out.println(randomInteger[indexPosition]); (or without the system.out.printnln

}

编辑@Debosmit Ray,因为我无法将其纳入评论。好的,非常感谢帮助它真正帮助我告诉我如何分解我的方法。我只有两个问题/问题。我在break;之后添加了System.out.println("Value at index: " + num[indx] );,因此不会有无限循环。我在System.out.println("Please enter a valid integer between 0 and 100 or type quit to exit: ");之后添加了} catch and the exceptions。我如何做到这一点,当我运行它并键入'bob'时,它只显示我的System.out.println("Please enter a valid integer between 0 and 100 or type quit to exit: ");语句而不是我的语句和原始循环语句"Please enter a valid integer between 0 and 100 or type quit to exit: "。第2,我无法让IndexOutOfBound exception工作,它只是崩溃

1 个答案:

答案 0 :(得分:0)

这是关于如何将代码分解为多个部分的简要介绍。如果有任何问题,请告诉我。我还没有写出你提到的所有内容,我觉得你能够自己解决这些问题。请在运行前仔细阅读代码。如果您需要任何帮助,请告诉我。 :)

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

public class HelloArcher {

    private static int[] randomInteger;

    public static void main(String[] args) {
        int[] num = getRandArray(100);
        Scanner s = new Scanner(System.in); 
        while(true) {
            int indx = getUserIndex(s);
            System.out.println("Index:" + indx);

            if(indx < 0 || indx > num.length) {
                // throw new ArrayIndexOutOfBoundsException("Wrong index bro");
                continue;
            }    
            System.out.println("Value at index: " + num[indx] );
        }
    }

    public static int[] getRandArray(int n) {
        int[] randomInteger = new int[n];
        Random rand = new Random();
        for (int i = 0; i < randomInteger.length; i++)
            randomInteger[i] = rand.nextInt();

        return randomInteger;
    }

    public static int getUserIndex(Scanner s) {
        System.out.println("Please enter a valid integer between 0 and 100 or type quit to exit: ");
        String in = s.next();
        if(in.equals("quit")) 
            System.exit(0);

        int indx = -1;

        try { 
            indx = Integer.parseInt(in);
        } catch (InputMismatchException | NumberFormatException | IndexOutOfBoundsException ex) {
            // weird value entered - run function again; quit is the only way to exit
            indx = getUserIndex(s);
        }
        return indx;
    }
}

一些示例输出

  

sh-4.3 $ javac HelloArcher.java
  sh-4.3 $ java -Xmx128M -Xms16M HelloArcher
  请输入0到100之间的有效整数或输入quit退出:
  5
  指数:5
  指数值:-472718090
  请输入介于0和100之间的有效整数,或输入quit退出:

     

-1指数:-1
  线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:   错误的指数兄弟

    at HelloArcher.main(HelloArcher.java:17)                                                                       

测试你'bob'输入

  

sh-4.3 $ java -Xmx128M -Xms16M HelloArcher
  请输入0到100之间的有效整数或输入quit退出:
  鲍勃
  请输入0到100之间的有效整数或输入quit退出:
  1
  指数:1个
  指数值:1138796751
  请输入0到100之间的有效整数或输入quit退出:
  退出