【发布时间】: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 center,How to Ask)。
-
你试过
toUpperCase()吗?
标签: java loops for-loop foreach