최소 및 최대 및 배열 배열 및 최대 및 최소 (int 또는 double)에 대한 사용자 입력에 따라 asks 사용하도록 프로그램을 만들려고 해요 배열 배열을 만듭니다. int 또는 double. 내 코드의 문제는 실행하려고 할 때 컴파일러에서 변수가 초기화되지 않았다고하는 것입니다. 오버로드 된 메서드를 만드는 것이 더 낫다고 가정하고 있지만 확실하지 않습니다.Java 만들기 int 배열 또는 double 배열
import java.util.Scanner;
public class RandomGames{
public static void main(String [] args){
randomArray();
}//end of main
public static void randomArray(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the length of desired array: ");
int length = input.nextInt();
int [] randomNumbers = new int [length];
System.out.println("Please enter the highest number: ");
if (input.hasNextInt()){
int max = input.nextInt();
}
else if (input.hasNextDouble()){
double max = input.nextDouble();
}
System.out.println("Please enter the Lowest number: ");
if (input.hasNextInt()){
int min = input.nextInt();
}
else if (input.hasNextDouble()){
double min = input.nextDouble();
}
arrayReturn(max, min);
} //end of randomArray
public static void arrayReturn(int max, int min){
System.out.println("This will return "+max+"min :"+ min +"in int");
}
public static void arrayReturn(double max, double min){
System.out.println("This will return "+max+"min :"+ min +"in double");
}
}
이것은 코멘트 일 것입니다. –
문제는 if 문 밖에서 초기화하면 int 또는 double 중 하나의 변수 유형으로 제한된다는 것입니다. – Aria
"초기화 됨"과 "선언 됨"을 혼동하지 마십시오. 외부에 선언했다면 내부에서 초기화하는 것이 좋습니다. 이 대답은 실제로 옳지 않습니다. 그리고이 코드보다 원래 코드의 문제가 훨씬 많습니다. –