gpt4 book ai didi

java - 方法太多?

转载 作者:行者123 更新时间:2023-12-05 07:52:49 24 4
gpt4 key购买 nike

<分区>

我刚写的这个类确实按照我想要的方式工作。我的问题是我是否将其分解为太多方法?它几乎看起来过分了,但在主要方法中看起来很拥挤。但是现在我有 6 个小方法(包括 main 在内有 7 个)。我想我正在寻找一般反馈。非常感谢!

我是否应该对本类(class)中发生的事情做一个简短的解释,以便人们更好地理解?

import java.util.Scanner;
import java.util.Arrays;

public class DuplicateElimination
{
private static Scanner input = new Scanner(System.in);
private static int[] numberArray = new int[5];
private static int initialInput = 0;

public static void main(String[] args)
{
Arrays.fill(numberArray, 0); //initialize array

for (int x = 0; x < numberArray.length; x++) // perform 5x
{
initialInput =getNumber(); //get number from user

boolean inRange = checkRange(); // check if number in range
if (inRange == true)
{
boolean uniqueNumber = uniqueNumber();//check if number is unique
if (uniqueNumber == true)
{
numberArray[0]=initialInput; //add number to array
printUniqueNumbers();

}
else
notUniqueNumber();
}
else
{
notInRange();
x--; // do not count out-of-range numbers as one of the 5 numbers
}
}//end for
}//end main

public static int getNumber()
{
System.out.printf("Please enter a unique number between 10 and 100: ");
int initialInput = input.nextInt();
return initialInput;
}//end getNumber

public static boolean checkRange()
{
boolean inRange;
if ((initialInput >=10) && (initialInput <= 100))
inRange = true;
else
inRange = false;
return inRange;
}//end checkRange

public static boolean uniqueNumber()
{
boolean uniqueNumber;
Arrays.sort(numberArray);
int location = Arrays.binarySearch(numberArray, initialInput);
if (location <= 0)
uniqueNumber = true;
else
uniqueNumber = false;
return uniqueNumber;

}//end uniqueNumber

public static void printUniqueNumbers()
{
System.out.println("List of unique numbers entered: ");
for (int n = 0; n < numberArray.length; n++)
if (numberArray[n] != 0)
System.out.print(numberArray[n] + " ");
System.out.println("\n");
}//end printUniqueNumbers

public static void notUniqueNumber()
{
System.out.print("You already input " + initialInput + ".\n\n");
printUniqueNumbers();
}

public static void notInRange()
{
System.out.println("Number is out of range. Please try again.\n");
}

}//end class

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com