java - 当我有 1 时,方法在下一行给我 2 输入
问题描述
我正在研究一个使用数组列表创建拼图的类,我遇到了 FillWords 方法的问题,这是一种要求用户输入单词以填充益智游戏的 2D ArrayList 的方法。当我运行代码时,当我下一行只有一个输入时,这种方法会在下一行给我双输入?
[![Image1]][1][1]: https://i.stack.imgur.com/jpoZ9.png
import java.util.*;
public class simplePuzzle
{
static Scanner input = new Scanner(System.in);
static Random rd = new Random();
public static void main(String[] args)
{
char[][] words;
int size;
displayPurpose();
size = getSize();
words = new char[size][size];
fillWords(words,size);
//displayWords(words,size);
}//of main
/**
* This method displays the main purpose of this program
*/
private static void displayPurpose()
{
System.out.println("----------------------------------------------------------------------------------");
System.out.println(" This program creates a simple word puzzle ");
System.out.println("----------------------------------------------------------------------------------");
}//of displayPurpose
/**
* This method asks the user for a size input
* @param size the size of the puzzle
*/
private static int getSize()
{
//asks the user for the size of the puzzle and checks if the input is between 5 and 10, stores the var in an int dataType and returns it to size
int size;
System.out.print("What is the size of the puzzle you would like to create (>=5 and <= 10): ");
size = input.nextInt();
if (size >= 5 && size <= 10 )
{
return size;
}
else
{
System.out.println("******Invalid size, MUST be ()>= 5 and <= 10)****** ");
System.out.print("What is the size of the puzzle you would like to create (>=5 and <= 10): ");
size = input.nextInt();
return size;
}
}//getSize
/**
* This method asks the user for puzzle words and stores them in the 2d array list
*/
private static void fillWords(char[][] words, int size)
{
// Asks the user for puzzle words, word cannot exteed the size of the puzzle and the letter of the word starts at random number on the puzzle grid.
String puzzleWord;
int wordLength;
int wordStart;
System.out.printf("%nEnter %d words, each word should be no more than %d characters long", size, size );
System.out.println("");
for(int i= 0; i < size; i++)
{
do{
System.out.println("Please enter a word:" );
puzzleWord = input.nextLine();
if (puzzleWord.length() > size){
System.out.printf("Word cannot fit in the puzzle. It MUST be <= %d characters in length", size);
}
}while(puzzleWord.length() > size);
for(int k= 0; k < puzzleWord.length(); k++){
wordLength = puzzleWord.length();
wordStart = size - wordLength ;
int v = rd.nextInt(wordStart) + 0;
//------------------------MAX----MIN
words[i][k]= puzzleWord.charAt(v);
}
}
}//fillWords
}//end
解决方案
我认为您的扫描仪有问题,请添加
input.nextLine();
当你读取尺寸后的行。它应该看起来像:
int option = input.nextInt();
input.nextLine();
推荐阅读
- javascript - 类型错误:使用 ftp-deploy 时无法读取未定义的属性“减少”
- java - PDFBox PDFMergerUtility 在 JavaFX 应用程序上不稳定
- sql - SQL:使用连接选择右表中的记录计数
- c# - 使用 OWIN 托管 Swagger UI 3.0 Dist 文件夹
- firebase - Firebase 网址格式
- python - Dialogflow v2 错误“必须设置 MalformedResponse 'final_response'”
- css - 如何使用媒体查询增加图像的大小
- visual-studio-code - 如何列出具有特定扩展名的所有文件
- reactjs - 可以有dataloader和rest api缓存吗?
- c# - 在c#中从日期时间中删除时间