gpt4 book ai didi

java - 带有 switch 语句的基本数组

转载 作者:行者123 更新时间:2023-12-01 14:05:37 25 4
gpt4 key购买 nike

我在这里有我的代码,我正在做一些简单的数组工作。原来这是我工作的输出:

  • Output:
  • Enter an item: _ // I don't know why I should have this.
  • Main Menu
    1. Add Item //Display List
    2. Remove Item //Display List; And searching for that particular item to be removed.
    3. Sort Item //Listed
      • Ascending Order
      • Descending Order
    4. Delete List //Should return to option 1 which is Add item
    5. Exit
import java.util.Scanner;
import java.util.Arrays;
import java.util.*;

public class MainArray{

public static void main(String[] args){
Scanner user_input = new Scanner(System.in);
int choice;
int array[] = new int[10]; //Default Size.

choice = menu(user_input);
while(choice != 4){
switch (choice){
case 1: array = addItems(array, user_input); break;
case 2: array = removeItems(array, user_input); break;
case 3: array = sort(array); break;
default: break;
}

choice = menu(user_input);
}
System.out.println("The End");
}

public static int menu(Scanner user_input) {
System.out.println("\n-------");
System.out.println("Here are your choices: \n" + "1: Add Items " + "2: Remove Items " + "3: Sort Items " + "4: End");
System.out.println("-------");
int choice = user_input.nextInt();
return choice;
}

public static int[] addItems(int array[], Scanner user_input){
for(int i = 0; i < array.length; i ++){
System.out.print("Value #" + (i + 1) + ": ");
array[i] = user_input.nextInt();
}
return array;
}

public static int[] removeItems(int array[], Scanner user_input){
System.out.println("Number to be removed: ");
for(int i = 0; i < array.length; i ++){
System.out.println("Item removed: " + array[i]);
try {
array[i] = user_input.nextInt();
}
catch(Exception e){
array[array.length - 1] = 0;
}
}
return array;
}

public static int[] sort(int array[]){
System.out.println("Numbers in Ascending Order:");
for(int i = 0; i < array.length; i ++){
Arrays.sort(array);
System.out.print(" " + array[i]);
}
System.out.println("\nNumbers in Descending Order:");
for(int i = array.length - 1; i >= 0; i --){
System.out.print(" " + array[i]);
}
return array;
}
}

所以现在我需要一些关于如何正确删除我的项目的帮助。我很清楚我的代码是错误的,有些是正确的,但大多数都是混淆的。基本上是这样的:

  1. 如何删除列表中的某些项目(例如 Value#1: 50、Value#2: 90、...、Value#10: 1),我想删除 ex 中的某些项目。值#7。
  2. 如何正确显示列表中当前/之前和之后的内容。
  3. 删除我的列表并让您返回到选项 1,即“添加项目”。

最佳答案

因为您尝试在创建整数集合后对其进行操作,所以需要使用另一种类型的集合,例如 ArrayList。您可以通过以下方式初始化此类集合:

List<Integer> aList = new ArrayList<Integer>();

请注意,集合需要 Integer 对象,但在大多数情况下,自动装箱和自动拆箱将帮助您传递和检索集合中的项目。您可以像以前一样 add() 从用户读取的整数到 aList,Java 会为您将其包装为 Integer 对象。

创建此集合后,您可以轻松添加()和删除()内容。 remove() 方法只接受您要删除的对象,因此您甚至不需要知道该对象在集合中的索引。事实上,您还可以在 ArrayList 上调用一个clear()方法,它将完成您提到的“删除”所有已添加的项目并重新开始。

现在,关于您的循环......您在removeItems()中迭代列表的长度并要求每次删除一个项目似乎很尴尬。如果用户只想删除单个项目怎么办?最好完全删除 for 循环,并让用户根据需要从菜单中选择该选项来决定要删除的项目数量。

最后,如果您切换到 ArrayList 方法,则在您的 sort() 方法中,您将需要在 ArrayList 上使用 Collections.sort() 方法。再次强调,无需将 Collections.sort() 调用放在 for 循环中。这是一件低效的事情,因为您只需要排序一次。

这是我正在讨论的方法的一个示例。您将想要尝试添加一些输入验证等,但它可以说明如何使用 ArrayList 来完成您想要做的事情:

public class Test{

public static void main(String[] args){
Scanner user_input = new Scanner(System.in);
int choice;

List<Integer> aList = new ArrayList<>();

choice = menu(user_input);
while(choice != 4){
switch (choice){
case 1: aList = Test.addItems(aList, user_input); break;
case 2: aList = Test.removeItems(aList, user_input); break;
case 3: Test.sort(aList); break;
default: break;
}

choice = menu(user_input);
}
System.out.println("The End");
}

public static int menu(Scanner user_input) {
System.out.println("\n-------");
System.out.println("Here are your choices: \n" + "1: Add Items "
+ "2: Remove Items " + "3: Sort Items " + "4: End");
System.out.println("-------");
int choice = user_input.nextInt();
return choice;
}

public static List<Integer> addItems(List<Integer> nums, Scanner user_input){
nums.clear();
for(int i = 0; i < 10; i ++){
System.out.print("Value #" + (i + 1) + ": ");
nums.add(user_input.nextInt());
}
return nums;
}

public static List<Integer> removeItems(List<Integer> nums, Scanner user_input){
System.out.println("Number to be removed: ");

Integer remove = user_input.nextInt();
if (nums.contains(remove)) {
nums.remove(remove);
System.out.println("Item removed: " + remove);
}
else {
System.out.println("Number not found.");
}
return nums;
}

public static void sort(List<Integer> nums){
System.out.println("Numbers in Ascending Order:");
Collections.sort(nums);
for(int i = 0; i < nums.size(); i++) {
System.out.print(" " + nums.get(i));
}

System.out.println("\nNumbers in Descending Order:");
for(int j = nums.size() - 1; j >= 0; j --){
System.out.print(" " + nums.get(j));
}
}
}

有关列表界面本身的更多信息:

http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html

关于java - 带有 switch 语句的基本数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18936044/

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