Я вызвал несколько разных методов в основном методе, который по сути является «составной» версией файла, объединяя эти существующие методы в один метод. Каждый метод, вызываемый в методе 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)