【问题标题】:Print out an array in uppercase Java用大写Java打印出一个数组
【发布时间】:2017-11-15 18:54:05
【问题描述】:

所以对于我目前正在创建的这个程序,我必须将 10 个名称存储在一个数组中,这很简单,然后打印出该数组,其中 10 个名称以大写形式显示,这是我目前卡住的部分在。有人告诉我,使用 for 或 for each 循环将能够实现这一点。

这是我目前拥有的代码:

package uploadTask8_uppercaseArrays;

import java.util.Arrays;
import java.util.Scanner;

public class uppercaseArrays {

    public static void main(String[] args) {
        // creates array that will store the 10 names
        String[] names10 = new String[10];

        System.out.println("Please enter the ten names: \n1: ");
        // create scanner object to get user input
        Scanner userInput = new Scanner(System.in);
        String firstName = userInput.nextLine();
        System.out.println("2: ");
        String secondName = userInput.nextLine();
        System.out.println("3: ");
        String thirdName = userInput.nextLine();
        System.out.println("4: ");
        String fourthName = userInput.nextLine();
        System.out.println("5: ");
        String fifthName = userInput.nextLine();
        System.out.println("6: ");
        String sixthName = userInput.nextLine();
        System.out.println("7: ");
        String seventhName = userInput.nextLine();
        System.out.println("8: ");
        String eigthName = userInput.nextLine();
        System.out.println("9: ");
        String ninthName = userInput.nextLine();
        System.out.println("10: ");
        String tenthName = userInput.nextLine();
        userInput.close();

        // assigns user input to each element in the array
        names10[0] = firstName;
        names10[1] = secondName;
        names10[2] = thirdName;
        names10[3] = fourthName;
        names10[4] = fifthName;
        names10[5] = sixthName;
        names10[6] = seventhName;
        names10[7] = eigthName;
        names10[8] = ninthName;
        names10[9] = tenthName;

        // prints out the array in lowercase
        System.out.println(Arrays.toString(names10));
        // prints out the array in uppercase



    }

}

我查看了一些其他代码,但实际上无法计算出用于将数组更改为大写的 for 或 for each 循环的代码。有谁知道这将如何工作?

【问题讨论】:

  • 你需要这些中间变量做什么?你可以直接names10[0] = userinput....。另外,这样你可以只使用一个循环。
  • 请求家庭作业帮助的问题必须包括您迄今为止为解决问题所做的工作摘要,以及对您在解决它时遇到的困难(help centerHow to Ask)。
  • 你试过toUpperCase()吗?

标签: java loops for-loop foreach


【解决方案1】:

实际上无法计算出 for 或 for each 循环的代码 将数组更改为大写。有谁知道这会如何 工作吗?

您可以在将字符串变量存储到数组时简单地将它们转换为大写。

    names10[0] = firstName.toUpperCase();
    names10[1] = secondName.toUpperCase();
    ...

或者更优雅的选择是运行如下循环:

for (int i = 0; i < names10.length; i++) {
    System.out.println((i + 1) + ": ");
    names10[i] = userInput.nextLine().toUpperCase();
}

【讨论】:

    【解决方案2】:

    您可以使用toUpperCase() 获取大写。

    您的代码太大,效率不高。你应该为此使用循环。像下面这样让它更短怎么样:

    public static void main(String[] args) {
            // creates array that will store the 10 names
            String[] names10 = new String[10];
    
            System.out.println("Please enter the ten names: \n");
            // create scanner object to get user input
    
            Scanner userInput = new Scanner(System.in);
            for(int i=1;i<=10;i++)
            {
                System.out.println(i+": ");
                names10[i-1] = userInput.nextLine().toUpperCase();
            }
    
            for(int i =1;i<=10;i++)
                System.out.println(names10[i-1]);
        }
    

    【讨论】:

      【解决方案3】:

      您可以直接读取值并将它们大写:

      String firstName = userInput.nextLine().toUpperCase();
      

      【讨论】:

        【解决方案4】:

        说明

        使用String#toUpperCase (documentation) 函数可以轻松地将String 转换为它们的大写 - 等效项,类似于String#toLowerCase (documentation) 方法。

        所以你需要访问数组的每个元素,然后调用它的方法:

        // Print in uppercase
        for (String name : names) {
            System.out.println(name.toUpperCase());
        }
        

        或者,如果您不熟悉 enhanced-for (foreach),使用常规 for 循环的版本:

        // Print in uppercase
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i].toUpperCase());
        }
        

        经过改进的完整代码

        您也不需要保存所有这些变量,只需将它们存储在数组中即可。之后循环数组并转换变量:

        public static void main(String[] args) {
            String[] names = new String[10];
        
            // Ask for names 10-times
            System.out.println("Please enter the ten names:");
            // Use try-with-resource for scanner-management
            try (Scanner userInput = new Scanner(System.in)) {
                for (int i = 0; i < 10; i++) {
                    System.out.println(i + ": ");
                    // Ensure that message is readable before
                    // asking for input
                    System.out.flush();
        
                    // Store the input
                    names[i] = userInput.nextLine();
                }
            }
            // Scanner is auto-closed due to try-with-resource
        
            // Print in lowercase
            for (String name : names) {
                System.out.println(name.toLowerCase());
            }
        
            // Print in uppercase
            for (String name : names) {
                System.out.println(name.toUpperCase());
            }
        }
        

        语言环境

        您甚至可以设置Locale 对象,因为在某些语言中大小写可能会发生变化。有关示例,请参阅documentation

        【讨论】:

          【解决方案5】:

          改变

          // prints out the array in lowercase
          System.out.println(Arrays.toString(names10));
          

          到:

          // prints out the array in uppercase
          System.out.println(Arrays.toString(names10).toUpperCase());
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多