我希望输入存储在数组中,并在一行中打印该特定int的字符串

我希望整数存储在数组中,然后一旦用户输入0,while循环将停止并以字符串格式打印数组的内容。


我尝试了这个地区的东西,但老实说我不理解他们。


public static void apples() {

    Scanner input = new Scanner(System.in);


    System.out.println("Enter your integer: ");

    int number = input.nextInt();

    int[] array = {};


    while(number != 0) {

        if(number == 1){

            System.out.println("0");

        }

        else if (number == 3) {

            System.out.println("f");

        }

        else if (number == 5) {

            System.out.println("-l");

        }

        else if (number == 7) {

            System.out.println("V");

        }

        else if (number == 9) {

            System.out.println("-/");

        }

        else if (number == 11) {

            System.out.println("()");

        }

        else if (number == 13) {

            System.out.println("()");

        }

        else if (number == 15) {

            System.out.println("^");

        }

        else if (number == 17) {

            System.out.println("E");

        }

        else if (number == 19) {

            System.out.println("=");

        }

        else if (number == 21) {

            System.out.println("x");

        }

        else if (number == 23) {

            System.out.println("y");

        }

        System.out.println("int: " +  number);

    }

};

我的预期结果是输入135,输出为0f-l


Helenr
浏览 103回答 3
3回答

倚天杖

请看一下 和 的这个简单用法示例。MapList&nbsp;public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Map<Integer, String> map = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; map.put(0, "");&nbsp; &nbsp; &nbsp; &nbsp; map.put(1, "0");&nbsp; &nbsp; &nbsp; &nbsp; map.put(3, "f");&nbsp; &nbsp; &nbsp; &nbsp; map.put(5, "-1");&nbsp; &nbsp; &nbsp; &nbsp; map.put(7, "V");&nbsp; &nbsp; &nbsp; &nbsp; map.put(9, "-/");&nbsp; &nbsp; &nbsp; &nbsp; map.put(11, "()");&nbsp; &nbsp; &nbsp; &nbsp; map.put(13, "()");&nbsp; &nbsp; &nbsp; &nbsp; map.put(15, "^");&nbsp; &nbsp; &nbsp; &nbsp; map.put(17, "E");&nbsp; &nbsp; &nbsp; &nbsp; map.put(19, "=");&nbsp; &nbsp; &nbsp; &nbsp; map.put(21, "x");&nbsp; &nbsp; &nbsp; &nbsp; map.put(23, "y");&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> listOfInputIntegers = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int integer;&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Input your next number:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfInputIntegers.add(integer);&nbsp; &nbsp; &nbsp; &nbsp; } while (integer != 0);&nbsp; &nbsp; &nbsp; &nbsp; for (int i : listOfInputIntegers) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(map.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }https://docs.oracle.com/javase/8/docs/api/java/util/Map.htmlhttps://docs.oracle.com/javase/8/docs/api/java/util/List.html

慕后森

将整数到字符串映射保留在 java.util.Map 中,以便在用户键入 int 后轻松获取相应的字符串。如果键入的字符串具有相应的字符串,请将其添加到要打印的字符串列表中。如果键入的字符串没有相应的字符串,请打印它并要求下一个intintint在用户键入 0 停止循环后,通过联接列表中的所有字符串,从累积的字符串列表中创建一个字符串,直到用户键入 0public class Test {&nbsp; &nbsp; private static Map<Integer, String> integerToStringMappings = new HashMap<>();&nbsp; &nbsp; static {&nbsp; &nbsp; &nbsp; &nbsp; integerToStringMappings.put(1, "0");&nbsp; &nbsp; &nbsp; &nbsp; integerToStringMappings.put(3, "f");&nbsp; &nbsp; &nbsp; &nbsp; integerToStringMappings.put(5, "-l");&nbsp; &nbsp; &nbsp; &nbsp; .... // and so on for all the integers mapped to strings&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> strings = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Integer number = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; while(number != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter your integer: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String correspondingString = integerToStringMappings.get(number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( correspondingString == null ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("int: " +&nbsp; number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strings.add(correspondingString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.join("", strings));&nbsp; &nbsp; }}

吃鸡游戏

这可以通过使用 ArrayList 轻松实现,因为您不知道数组的大小(除非您专门询问用户将输入的项目数)。收集用户的数字后,只需遍历 ArrayList 中的每个数字,即可使用 if-else 语句生成相应的输出。在这种情况下,我强烈建议使用 switch 语句。ArrayList<Integer> values = new ArrayList<Integer>();int number = input.nextInt();while(number != 0) {&nbsp; &nbsp; values.add(number);&nbsp; &nbsp; number = input.nextInt();}for(int i=0; i<values.size(); i++) {&nbsp; &nbsp; int numberCheck = values.get(i);&nbsp; &nbsp; //Run through the if-else statements using numberCheck&nbsp; &nbsp; System.out.println("int: " + numberCheck);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java