Java 向阵列菜单中添加数字
我有个问题。我刚开始学习java中的数组。我了解数组以及它们是如何工作的,非常了解如何存储数据。但我的问题是,我试图通过使用菜单向数组中添加一个现有整数。但是,我不明白为什么我的程序似乎不起作用Java 向阵列菜单中添加数字,java,Java,我有个问题。我刚开始学习java中的数组。我了解数组以及它们是如何工作的,非常了解如何存储数据。但我的问题是,我试图通过使用菜单向数组中添加一个现有整数。但是,我不明白为什么我的程序似乎不起作用 import java.util.Scanner; public class ArraySorting { public static void main(String[] args) { int option; int integer = 0; int option
import java.util.Scanner;
public class ArraySorting
{
public static void main(String[] args)
{
int option;
int integer = 0;
int optionOne;
Scanner kb = new Scanner(System.in);
System.out.println("Please enter a non-negative integer: ");
integer = kb.nextInt();
while((integer < 0))
{
System.out.println("I am sorry that is not a non-negative integer.");
System.out.println("");
System.out.println("Please enter a non-negative integer: ");
integer = kb.nextInt();
}
option = displayMenu(kb);
while (option != 6)
{
switch (option)
{
case 1:
optionOne();
System.out.println("Done with Option 1. Please enter another option.");
break;
case 2:
//optionTwo();
System.out.println("Done with Option 2. Please enter another option.");
break;
case 3:
//optionThree();
System.out.println("Done with Option 3. Please enter another option.");
break;
case 4:
//optionFour();
System.out.println("Done with Option 4. Please enter another option.");
break;
case 5:
//optionFive();
System.out.println("Done with Option 5. Please enter another option.");
break;
}
option = displayMenu(kb);
}
if (option == 6)
{
System.out.println();
System.out.println("Thank you. Have a nice day.");
}
}
private static int displayMenu(Scanner kb)
{
int option = 0;
while (option != 1 && option != 2 && option != 3 && option != 4 && option !=5 && option !=6)
{
System.out.println("\t\t1. Add a number to the array\n\t\t2. Display the mean\n\t\t3. Display the median \n\t\t4. Print the array to the screen \n\t\t5. Print the array in reverse order \n\t\t6. Quit");
option = kb.nextInt();
if (!(option == 1 || option == 2 || option == 3 || option == 4 || option == 5 || option == 6))
System.out.println("I am sorry that is an invalid choice. Please try again.");
}
return option;
}
private static int optionOne()
{
Scanner input = new Scanner(System.in);
int[] numbers = new int[99];
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Please enter number");
numbers[i] = input.nextInt();
}
return numbers;
}
}
import java.util.Scanner;
公共类排列排序
{
公共静态void main(字符串[]args)
{
int选项;
整数=0;
int optionOne;
扫描仪kb=新扫描仪(System.in);
System.out.println(“请输入一个非负整数:”);
整数=kb.nextInt();
而((整数<0))
{
System.out.println(“很抱歉,这不是一个非负整数。”);
System.out.println(“”);
System.out.println(“请输入一个非负整数:”);
整数=kb.nextInt();
}
选项=显示菜单(kb);
while(选项!=6)
{
开关(选件)
{
案例1:
optionOne();
System.out.println(“使用选项1完成。请输入另一个选项”);
打破
案例2:
//optionTwo();
System.out.println(“使用选项2完成。请输入另一个选项”);
打破
案例3:
//OptionTree();
System.out.println(“使用选项3完成。请输入另一个选项”);
打破
案例4:
//选项四();
System.out.println(“使用选项4完成。请输入另一个选项”);
打破
案例5:
//optionFive();
System.out.println(“使用选项5完成。请输入另一个选项”);
打破
}
选项=显示菜单(kb);
}
如果(选项==6)
{
System.out.println();
System.out.println(“谢谢你,祝你愉快。”);
}
}
专用静态int显示菜单(扫描仪kb)
{
int选项=0;
而(选项!=1&&option!=2&&option!=3&&option!=4&&option!=5&&option!=6)
{
System.out.println(“\t\t1.向数组中添加一个数字\n\t\t2.显示平均值\n\t\t3.显示中值\n\t\t4.将数组打印到屏幕上\n\t\t5.按相反顺序打印数组\n\t\t6.退出”);
option=kb.nextInt();
如果(!(选项==1 | |选项==2 | |选项==3 | |选项==4 | |选项==5 | |选项==6))
System.out.println(“很抱歉,这是一个无效的选择。请重试。”);
}
返回选项;
}
专用静态int optionOne()
{
扫描仪输入=新扫描仪(System.in);
整数[]个数=新整数[99];
for(int i=0;i
代码有故障
问题是你的函数optionOne应该返回一个整数数组。您已经将返回类型设置为一个整数
其中之一需要改变
如果必须返回数组,我认为是这样,那么函数应该是这样的
private static int[] optionOne()
{
Scanner input = new Scanner(System.in);
int[] numbers = new int[99];
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Please enter number");
numbers[i] = input.nextInt();
}
return numbers;
}
private static int[]optionOne()
{
扫描仪输入=新扫描仪(System.in);
整数[]个数=新整数[99];
for(int i=0;i
干杯。什么不起作用?帮助我们,帮助我们you@ScaryWombat对不起,谢谢你!但是它似乎一直在运行,并且没有停止并将用户返回到菜单?您已经创建了一个包含99个数字的数组,并且正在根据其长度进行遍历,因此,您的代码将请求99次数字…您必须更改它。