Java For循环:从用户输入填充数组
我正在设置一个小应用程序,要求用户确定数组大小,然后填充它。使用的“for”循环跳过索引0;但我不确定为什么 如果以1作为数组大小运行此代码,它将跳过输入第一个单词的用户 这个问题当然在for循环中,但它太简单了,我看不到它。 谢谢Java For循环:从用户输入填充数组,java,arrays,for-loop,Java,Arrays,For Loop,我正在设置一个小应用程序,要求用户确定数组大小,然后填充它。使用的“for”循环跳过索引0;但我不确定为什么 如果以1作为数组大小运行此代码,它将跳过输入第一个单词的用户 这个问题当然在for循环中,但它太简单了,我看不到它。 谢谢 import java.util.Scanner; 公共类WordRandomizerAdvanced{ 公共静态void main(字符串[]args){ 数组维数; 扫描仪sc=新的扫描仪(System.in); System.out.println(“****
import java.util.Scanner;
公共类WordRandomizerAdvanced{
公共静态void main(字符串[]args){
数组维数;
扫描仪sc=新的扫描仪(System.in);
System.out.println(“*******************************************************************************”);
System.out.println(“******欢迎使用Word随机化器高级*******”;
System.out.println(“*******************************************************************************”);
//获取数组大小
System.out.println(“您想输入多少单词?”);
arraydimision=sc.nextInt();
String[]wordArray=新字符串[ArrayDimension];
//使用用户输入填充
对于(int i=0;i更改
arrayDimesion = sc.nextInt();
到
原因:sc.nextLine()
不会使用您在接受arraydimision
输入后给出的换行符。这将在下一次sc.nextLine()
调用中使用
PS:它可能会抛出。因此,您可以像以下方式处理它:
try {
arrayDimesion = Integer.parseInt(sc.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}
下面的代码是干净的,易于阅读和处理的边缘情况
import java.util.Scanner;
public class WordRandomizerAdvanced {
public static void main(String[] args) {
int numOfWords;
Scanner scanner = new Scanner(System.in);
System.out.println("****************************************************");
System.out.println("******** Welcome to Word Randomizer ADVANCED********");
System.out.println("****************************************************");
//Get array size
System.out.println("How many words would you like to enter?");
numOfWords = Integer.parseInt(scanner.nextLine());
String[] wordArray = new String[numOfWords];
//Populate with user input
System.out.println("Please enter the word(s)");
for (int i = 0; i < numOfWords; i++) {
wordArray[i] = scanner.nextLine();
}
//Print all entered Strings
System.out.println("These are the words you entered: ");
for (int i = 0; i < numOfWords; i++) {
System.out.println(wordArray[i]);
}
//Print random string from array
if (numOfWords == 0) {
System.out.println("You didn't enter a word");
} else {
int r = (int) (Math.random() * numOfWords);
System.out.println("The random word is: " + wordArray[r]);
}
}
}
import java.util.Scanner;
公共类WordRandomizerAdvanced{
公共静态void main(字符串[]args){
整数字;
扫描仪=新的扫描仪(System.in);
System.out.println(“*******************************************************************************”);
System.out.println(“******欢迎使用Word随机化器高级*******”;
System.out.println(“*******************************************************************************”);
//获取数组大小
System.out.println(“您想输入多少单词?”);
numOfWords=Integer.parseInt(scanner.nextLine());
String[]wordArray=新字符串[numOfWords];
//使用用户输入填充
System.out.println(“请输入单词”);
for(int i=0;i
第1步:在调试器中一行一行地执行代码,看看发生了什么。“非常简单,你看不到它”,但很容易看出你是否在IDE的调试器中逐行执行。一些解释很好。比如说新行字符没有被使用。@SebastiaanvandenBroek我刚刚编辑了相同的。Ty btw。
try {
arrayDimesion = Integer.parseInt(sc.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}
import java.util.Scanner;
public class WordRandomizerAdvanced {
public static void main(String[] args) {
int numOfWords;
Scanner scanner = new Scanner(System.in);
System.out.println("****************************************************");
System.out.println("******** Welcome to Word Randomizer ADVANCED********");
System.out.println("****************************************************");
//Get array size
System.out.println("How many words would you like to enter?");
numOfWords = Integer.parseInt(scanner.nextLine());
String[] wordArray = new String[numOfWords];
//Populate with user input
System.out.println("Please enter the word(s)");
for (int i = 0; i < numOfWords; i++) {
wordArray[i] = scanner.nextLine();
}
//Print all entered Strings
System.out.println("These are the words you entered: ");
for (int i = 0; i < numOfWords; i++) {
System.out.println(wordArray[i]);
}
//Print random string from array
if (numOfWords == 0) {
System.out.println("You didn't enter a word");
} else {
int r = (int) (Math.random() * numOfWords);
System.out.println("The random word is: " + wordArray[r]);
}
}
}