创建一个数组列表,但在我的switch语句中涉及modifyfruit

时间:2016-12-11 18:41:27

标签: java

出于某种原因,我在switch语句中收到一条错误,指出我输入的参数没有创建。我的所有方法都是正确的,但我没有看到问题出在哪里。任何人都可以帮我解决这个问题吗?

package javaapplication7;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import java.awt.Event;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.jar.Attributes;


public class JavaApplication7 {
    private static ArrayList<String> charles = new ArrayList<String>();
    private static Scanner chris = new Scanner(System.in);
    private static boolean loops = true;
    public static void main(String[] args) {
        charles.add("Apple");
        charles.add("Orange");
        charles.add("Grapes");
        charles.add("Plums");
        charles.add("Grapefruit");
        charles.add("Kiwi");
        System.out.println("ArrayList Addons");
        System.out.println("==================");
        System.out.println(charles);
        System.out.println("");
        System.out.println("Choose a number");

        while(loops){

           System.out.println("Enter Select A Choice");
           System.out.println("====================");
           System.out.println("1 - End Program");
           System.out.println("2 - Add Fruit");
           System.out.println("3 - Remove Fruit");
           System.out.println("4 - Modify Fruit");
           System.out.println("5 - List of your fruit");

           int choice = chris.nextInt();

                switch(choice){
                    case 1:
                        loops = false;
                        break;
                    case 2:
                        addfruit();
                        break;
                    case 3:
                        removefruit();
                        break;
                    case 4:
                        modifyfruit(option, fruits);
                        break;
                    case 5:
                        printlist();
                        break;
                    default:
                        printlist();
                        System.out.println("***PLEASE SELECT FROM THE LIST***");
                        break;

            }

        }

    }
    public static void printlist(){
        System.out.println("Your list of furit");
        System.out.println(charles);
    }
    public static void addfruit(){
        System.out.println("Add another fruit");
        String trees = chris.next();
        charles.add(trees);
        System.out.println("You've successfully added "+trees);
        printlist();

    }
        public static void removefruit(){
        System.out.println("Select what fruit to remove");
        int removal = chris.nextInt();
        charles.remove(removal-1);
        System.out.println("You've successfully added "+removal);
        printlist();

    }
        public static void modifyfruit(int option, String fruits){
        System.out.println("Select what position to change");
        option = chris.nextInt();
        System.out.println("Select A new fruit to add");
        fruits = chris.next();
        charles.set(option+1, fruits);
        System.out.println("Your new fruit is "+ fruits);
        printlist();

    }
}

2 个答案:

答案 0 :(得分:-1)

您不需要modifyfruit方法中的参数。在调用方法和方法签名的位置删除它们,然后在方法中定义它们。

    public static void modifyfruit() {
        System.out.println("Select what position to change");
        int option = chris.nextInt();
        System.out.println("Select A new fruit to add");
        String fruits = chris.next();
        charles.set(option + 1, fruits);
        System.out.println("Your new fruit is " + fruits);
        printlist();
    }

答案 1 :(得分:-1)

开关内部:

case 4:
                    modifyfruit(option, fruits);
                    break;

选项和水果尚未创建,可以看到,当您定义方法modifyfruit()选项时,水果正在从用户输入。