用大写的Java打印出一个数组

时间:2017-11-15 18:54:06

标签: java loops for-loop foreach

所以对于这个程序,我目前正在创建我必须在一个数组中存储10个名字,这很简单,然后打印出那个10个名字以大写字母显示的数组,这是I&#39的部分;米目前坚持下去。我被告知使用for或for循环将能够实现此目的。

这是我目前的代码:

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或者每个循环的代码,这些代码会将数组更改为大写。有谁知道这会如何运作?

5 个答案:

答案 0 :(得分:1)

  

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

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

    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();
}

答案 1 :(得分:1)

您可以使用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]);
    }

答案 2 :(得分:1)

说明

使用Stringdocumentation)函数可以很容易地将String#toUpperCase转换为大写 - 等效,类似于{{1} }(documentation)方法。

因此,您需要访问数组的每个元素,然后在其上调用方法:

String#toLowerCase

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

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

包含改进的完整代码

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

// 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()); } } 对象,因为大写和小写可能会在某些语言中发生变化。有关示例,请参阅documentation

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

更改

import nw from 'nwjs'; //I guess this is not correct

为:

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