首页 > 解决方案 > 当以前调用它们时,如何在 main 中获得不同的方法以相互交互?

问题描述

我在 main 方法中调用了几个不同的方法,它本质上是文件的“复合”版本,将这些现有方法组合成一个方法。在 main 方法中调用的每个方法都有自己独特的属性,这些属性在同一文件中的 main 方法之外定义。main 中有 5 个方法,每个方法都是在 main 方法之外定义的,其中 int 和 Strings 在类级别上定义。

        import java.util.Scanner;
        public class Chapter5 
        {
          Scanner scan = new Scanner(System.in);

        int randomNumber1, randomNumber2, randomNumber3, randomNumber4, randomNumber5, randomNumber6;

        int counter = 0;

        int userInput1 = scan.nextInt();
        int userInput2 = scan.nextInt();
        int userInput3 = scan.nextInt();
        int userInput4 = scan.nextInt();
        int userInput5 = scan.nextInt();
        int userInput6 = scan.nextInt();

        int numberMatch = 0;
        int matchingIndex;

        public void main(String[] args)
        {
          GenerateRandomNumber();
          IsDuplicateNumber();
          GetUserInput();
          ValidateUserInputNumber();
          OutputResult();
        }

        public int GenerateRandomNumber(){

              randomNumber1 = (int) (Math.random() * 60) + 1;
              randomNumber2 = (int) (Math.random() * 60) + 1;
              randomNumber3 = (int) (Math.random() * 60) + 1;
              randomNumber4 = (int) (Math.random() * 60) + 1;
              randomNumber5 = (int) (Math.random() * 60) + 1;
              randomNumber6 = (int) (Math.random() * 60) + 1; 
              return GenerateRandomNumber();
            }

    public boolean IsDuplicateNumber() {
      if (randomNumber2 == randomNumber1) {
        randomNumber2 = (int)(Math.random() * 60) + 1;
        return true;}
      else if (randomNumber3 == randomNumber1 || randomNumber3 == randomNumber2){
        randomNumber3 = (int) (Math.random() * 60) + 1;
        return true;}
      else if (randomNumber4 == randomNumber1 || randomNumber4 == randomNumber2 || randomNumber4 == randomNumber3){
              randomNumber4 = (int) (Math.random() * 60) + 1;
              return true;}
      else if (randomNumber5 == randomNumber1 || randomNumber5 == randomNumber2 || randomNumber5 == randomNumber3 || randomNumber5 == randomNumber4){
              randomNumber5 = (int) (Math.random() * 60) + 1;
              return true;}
      else if (randomNumber6 == randomNumber1 || randomNumber6 == randomNumber2 || randomNumber6 == randomNumber3 || randomNumber6 == randomNumber4 || randomNumber6 == randomNumber5){
              randomNumber6 = (int) (Math.random() * 60) + 1;
              return true;}
      return true;
    }

    public void GetUserInput() {

          System.out.println("Enter a number between 1-60: ");
          userInput1 = scan.nextInt();
          System.out.println("Enter a number between 1-60: ");
          userInput2 = scan.nextInt();
          System.out.println("Enter a number between 1-60: ");
          userInput3 = scan.nextInt();
          System.out.println("Enter a number between 1-60: ");
          userInput4 = scan.nextInt();
          System.out.println("Enter a number between 1-60: ");
          userInput5 = scan.nextInt();
          System.out.println("Enter a number between 1-60: ");
          userInput6 = scan.nextInt();
    }

    public boolean ValidateUserInputNumber() {

      if (userInput1 > 60 || userInput1 < 1) {
        System.out.println("Invalid input");}
      if (userInput2 > 60 || userInput2 < 1) {
        System.out.println("Invalid input");}
      if (userInput3 > 60 || userInput3 < 1) {
        System.out.println("Invalid input");}
      if (userInput4 > 60 || userInput4 < 1) {
        System.out.println("Invalid input");}
      if (userInput5 > 60 || userInput5 < 1) {
        System.out.println("Invalid input");}
      if (userInput6 > 60 || userInput6 < 1) {
        System.out.println("Invalid input");}

      if (userInput2 == userInput1)
        while(userInput2 == userInput1)
      {
        System.out.println("Duplicate");
        userInput2 = scan.nextInt();
      }
      if (userInput3  == userInput1 || userInput3 == userInput2)
        while(userInput3  == userInput1 || userInput3 == userInput2)
      {
        System.out.println("Duplicate");
        userInput3=scan.nextInt();
      }
      if (userInput4 == userInput1 || userInput4 == userInput2 || userInput4 == userInput3)
        while(userInput4 == userInput1 || userInput4 == userInput2 || userInput4 == userInput3)
      {
        System.out.println("Duplicate");
        userInput4=scan.nextInt();
      }
      if (userInput5 == userInput1 || userInput5 == userInput2 || userInput5 == userInput3 || userInput5 == userInput4)
        while(userInput5 == userInput1 || userInput5 == userInput2 || userInput5 == userInput3 || userInput5 == userInput4)
      {
        System.out.println("Duplicate");
        userInput5=scan.nextInt();
      }
       if (userInput6 == userInput1 || userInput6 == userInput2 || userInput6 == userInput3 || userInput6 == userInput4 || userInput6 == userInput5)
        while(userInput6 == userInput1 || userInput6 == userInput2 || userInput6 == userInput3 || userInput6 == userInput4 || userInput6 == userInput5)
      {
        System.out.println("Duplicate");
        userInput6=scan.nextInt();
      }
      return true;
    }

    public int OutputResult() {
      getNumOfMatches();
     if (userInput1 == randomNumber1 || userInput2 == randomNumber2 || 
    userInput3 == randomNumber3 || userInput4 == randomNumber4 || userInput5 == 
    randomNumber5 || userInput6 == randomNumber6) {
                System.out.println("Exact match: you win $1,000,000");
            }
            else if (5 == getNumOfMatches()) {
                System.out.println("Match all digits: you win $100,000");
            }
            else if (4 == getNumOfMatches()) {
                System.out.println("Match all digits: you win $8,000");
            }
            else if (3 == getNumOfMatches()) {
                System.out.println("Match three digits: you win $20");
            }
            else if (2 == getNumOfMatches()) {
                System.out.println("Match two digits: you win $6");
            }
            else if (1 == getNumOfMatches()) {
                System.out.println("Match two digit: you win $2");
            }
            else {
                System.out.println("Sorry, no match; $0");
            }
      return OutputResult();

此外,我有两个在预先存在的方法中调用的方法。它们都在方法 OutputResult() 中调用;

      public boolean foundInLottery(int uInput)
        {
          if(uInput == randomNumber1 || uInput == randomNumber2 
                    || uInput == randomNumber3 || uInput == randomNumber4 
                    || uInput == randomNumber5 || uInput == randomNumber6)
            return true;
          else
            return false;
        }

        public int getNumOfMatches()
        {
          int iMatches = 0;
          if (foundInLottery(userInput1)) { iMatches =iMatches + 1 ;}
          if (foundInLottery(userInput2)) { iMatches =iMatches + 1 ;}
          if (foundInLottery(userInput3)) { iMatches =iMatches + 1 ;}
          if (foundInLottery(userInput4)) { iMatches =iMatches + 1 ;}
          if (foundInLottery(userInput5)) { iMatches =iMatches + 1 ;}
          if (foundInLottery(userInput6)) { iMatches =iMatches + 1 ;}

           return iMatches;
        }

该程序编译时没有错误或警告,但是当它运行时,输出窗口中的消息说

java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

标签: javamethods

解决方案


推荐阅读