사용자가 배열에 숫자를 추가하는 옵션이있는 메뉴를 만들려고하고 기술적으로 원하는 값의 수를 한 번에 하나씩 추가 할 수 있습니다. 따라서 배열에 숫자를 추가하기로 선택할 때 하나의 숫자를 입력하면 메뉴가 다시 표시됩니다. 배열을 표시하는 배열 &에 숫자를 추가하는 데 문제가 있습니다. 나는 두 개의 숫자를 더한다 & 어레이를 보게한다 & 내 배열이 최대이기 때문에 나는 30000000000000000000000000000000000000000000000000000000000이된다. 내가 원하는 건 어레이에 입력 한 자릿수 만 추가하는 것입니다. 입력 할 때 & 계속 입력하십시오.Java 배열 초보자 메뉴
정확히 어떻게 수정하겠습니까? 사용자가 배열의 크기 등을 완전히 제어 할 수 있기를 바랍니다.
import java.util.*;
public class arrayMenu
{
public static void main(String[] args)
{
Scanner kb;
int option;
kb = new Scanner(System.in);
option = menu(kb);
int[] myArray = new int[99];
while (option != 6)
{
switch (option)
{
case 1:
myArray = newNum(kb, myArray);
break;
case 2:
display(myArray);
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
option = menu(kb);
}
if (option == 6)
{
System.out.println();
System.out.println("Thank you. Have a nice day.");
}
}
public static int menu(Scanner kb)
{
int myOption = 0;
while (myOption != 1 && myOption != 2 && myOption != 3 && myOption != 4 && myOption != 5 && myOption != 6)
{
System.out.println();
System.out.println("Please select from the following menu choices.");
System.out.println();
System.out.println("1. Add a number to the array \n2. Display the mean \n3. Display the median \n4. Print the array to the screen \n5. Print the array in reverse order \n6. Quit");
System.out.println();
System.out.print("Choice --> ");
myOption = kb.nextInt();
kb.nextLine();
if (!(myOption == 1 || myOption == 2 || myOption == 3 || myOption == 4 || myOption == 5 || myOption == 6))
System.out.println("I am sorry that is an invalid menu choice.\nPlease try again");
}
return myOption;
}
public static int intNum(Scanner kb)
{
int num = -1;
while (!(num >= 0))
{
System.out.print("Please enter a non-negative integer -->");
num = kb.nextInt();
if (num < 0)
System.out.print("I am sorry that is not a non-negative integer. \n");
}
return num;
}
public static int[] newNum(Scanner kb, int[] array)
{
for (int i = 0; i < 1; i++)
{
System.out.print("Please enter a number:");
array[i] = kb.nextInt();
}
return array;
}
public static void display(int[] array)
{
for (int myValue : array)
{
System.out.print(myValue);
}
}
}
자바는 자바 스크립트가 아닙니다. 이 질문에서 javascript 태그를 제거해야합니다. – chiliNUT
정확히 어떻게 수정하겠습니까? 사용자가 어레이를 얼마나 크게 제어 할 수 있기를 원합니다. – user3230008