我遇到了一些困境,我需要更改我的数组,以便每行读取一个字符串而不是单个字符;但是,当我这样做时,它完全改变了我的整个程序。如果需要,我可以提供更多代码。
我尝试过hasNext和其他我能想到的东西。我已经举了一个我在下面的意思的例子
如果用户输入:2
比用户想要输入O,X,O,X
,但我想这样做,所以他们仍然可以输入字符,但每行都有多个字符。
我希望他们能够在一行上输入OX
,在下一行上输入OX
。
package mazeanalyze.java;
import java.util.*;
/**
*
* @author RexABBWJonathan
*/
public class MazeAnalyzeJava {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner kbd = new Scanner(System.in);
//int[][] totalOpenness = new int[n][n];
System.out.println("ENTER A SINGLE INTEGER: ");
int n = kbd.nextInt();
int[][] totalOpenness = new int[n+1][n+1];
char[][] mazeValue = new char[n + 1][n + 1];
System.out.println("ENTER A PATH: ");
for (int i = 0; i < mazeValue.length; i++) {
for (int j = 0; j < mazeValue[i].length; j++) {
if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
mazeValue[i][j] = 'X';
else {
mazeValue[i][j] = kbd.next().charAt(0);
}
}
}
printMaze(mazeValue);
System.out.println("");
openfactor(mazeValue, n);
System.out.println("");
horizontalPath(mazeValue, n);
System.out.println(" ");
verticalPath(mazeValue,n);
System.out.println(" ");
//openfactor(mazeValue, n);
//average(mazeValue,n,totalOpenness);
}
public static void printMaze(char mazeValue[][]) {
System.out.println("MAZE");
for (int i = 1; i < mazeValue.length; i++) {
for (int j = 1; j < mazeValue[i].length; j++) {
System.out.printf("%5c", mazeValue[i][j]);
}
System.out.printf("\n");
}
}
public static void horizontalPath(char mazeValue[][], int n) {
int[] totalRow = new int[n];
// int horizontalPath=0;
int count = 0;
for (int i = 1; i < mazeValue.length; i++) {
for (int j = 1; j < mazeValue[i].length; j++) {
if (mazeValue[i][j] == 'O') {
count++;
} else {
if (totalRow[i - 1] < count)
totalRow[i - 1] = count;
count = 0;
}
}
if (totalRow[i - 1] < count)
totalRow[i - 1] = count;
count = 0;
}
int biggestRow = totalRow[0];
// int longestRow=0;
int finalLongestRow = 0;
for (int x = 0; x < n; x++) {
if (biggestRow < totalRow[x]) {
biggestRow = totalRow[x];
finalLongestRow = x;
}
}
System.out.printf("Longest horizontal path row %d length %d", finalLongestRow + 1, biggestRow);
}
public static void verticalPath(char mazeValue[][], int n) {
int[] totalColumn = new int[n];
// int horizontalPath=0;
int count = 0;
for (int j = 1; j < mazeValue.length; j++){
for (int i = 1; i < mazeValue[j].length; i++) {
if (mazeValue[i][j] == 'O') {
count++;
} else {
if (totalColumn[j - 1] < count)
totalColumn[j - 1] = count;
count = 0;
}
}
if (totalColumn[j - 1] < count)
totalColumn[j - 1] = count;
count = 0;
}
int biggestColumn = totalColumn[0];
// int longestRow=0;
int finalLongestColumn = 0;
for (int x = 0; x < n; x++) {
if (biggestColumn < totalColumn[x]) {
biggestColumn = totalColumn[x];
finalLongestColumn = x;
}
}
//System.out.printf("Longest horizontal path row %d length %d", finalLongestColumn + 1, biggestColumn);
System.out.printf("Longest vertical path column %d length %d", finalLongestColumn + 1, biggestColumn);
}
public static void openfactor(char[][] mazeValue, int n)
{
//double rowAvg=0;
//double totalRowAvg=0;
int[][] totalOpenness = new int[n+1][n+1];
System.out.println("STATISTICS");
for(int i = 1; i<=n; i++)
{
double rowAvg=0;
double totalRowAvg=0;
for(int j=1;j<=n;j++)
{
int count=0;
int openness=0;
int totalRowOpeness = 0;
//double rowAvg=0;
if(mazeValue[i][j]=='X'){
count--;
}
else
{
//YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
if( j-1>=1)
{
if(mazeValue[i][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=1 && j-1>=1)
{
if(mazeValue[i-1][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=1)
{
if(mazeValue[i-1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<=n)
{
if(mazeValue[i][j+1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<=n && i+1<=n)
{
if(mazeValue[i+1][j+1]=='O')
count++;
}
if (i+1<=n)
{
if(mazeValue[i+1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j-1>=1 && i+1<=n)
{
if(mazeValue[i+1][j-1]=='O')
count++;
}
if(i-1>=1 && j+1<=n)
{
if(mazeValue[i-1][j+1]=='O')
count++;
}
//}//eND OF iF CONDITION\
}
openness = openness +count;
totalOpenness[i][j] = openness;
//System.out.println("TOTAL OPENESS FOR : [" + i + "]" +"[" + j + "] IS " +totalOpenness[i][j]);
System.out.printf("%5d",totalOpenness[i][j]);
totalRowOpeness = totalRowOpeness + totalOpenness[i][j];
rowAvg = (double)totalRowOpeness/(double)n;
totalRowAvg = totalRowAvg + rowAvg;
}
//System.out.println("TOTAL SUM ROW AVERAGE: " +totalRowAvg);
System.out.printf("%5.1f",totalRowAvg);
System.out.println("");
}
for(int j=1; j<=n;j++){
double ColumnAvg=0;
double totalColumnAvg=0;
for(int i=1; i<=n;i++){
int count=0;
int openness=0;
int totalColumnOpeness = 0;
if(mazeValue[i][j]=='X'){
count--;
}
else
{
//YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
if( j-1>=1)
{
if(mazeValue[i][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=1 && j-1>=1)
{
if(mazeValue[i-1][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=1)
{
if(mazeValue[i-1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<=n)
{
if(mazeValue[i][j+1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<=n && i+1<=n)
{
if(mazeValue[i+1][j+1]=='O')
count++;
}
if (i+1<=n)
{
if(mazeValue[i+1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j-1>=1 && i+1<=n)
{
if(mazeValue[i+1][j-1]=='O')
count++;
}
if(i-1>=1 && j+1<=n)
{
if(mazeValue[i-1][j+1]=='O')
count++;
}
//}//eND OF iF CONDITION\
}
openness = openness +count;
totalOpenness[j][i] = openness;
//System.out.println("TOTAL OPENESS FOR : [" + j + "]" +"[" + i + "] IS " +totalOpenness[j][i]);
totalColumnOpeness = totalColumnOpeness + totalOpenness[j][i];
ColumnAvg = (double)totalColumnOpeness/(double)n;
totalColumnAvg = totalColumnAvg + ColumnAvg;
}
//System.out.println("TOTAL SUM COLUMN AVERAGE: " +totalColumnAvg);
System.out.printf("%5.1f",totalColumnAvg);
}
}
}
答案 0 :(得分:0)
您遇到的问题是您正在调用&#34; kbd.next()。charAt(0);&#34;。这个问题是.next()得到一个字符串(在你的情况下&#34; oxxox&#34;或者什么)然后只得到第一个字符,所以之后的所有内容都会丢失。
我不知道用扫描仪读取字符的另一种方式。以下是我提出的main代码。它的作用是调用&#34; .next()&#34;然后迭代它。由于计数器i和j的运行方式与我们想要的不同。我们需要创建一个手动递增的本地计数器。
public static void main(String[] args) {
// TODO code application logic here
Scanner kbd = new Scanner(System.in);
// int[][] totalOpenness = new int[n][n];
System.out.println("ENTER A SINGLE INTEGER: ");
int n = kbd.nextInt();
int[][] totalOpenness = new int[n + 1][n + 1];
char[][] mazeValue = new char[n + 1][n + 1];
System.out.println("ENTER A PATH: ");
String input = ""; //Where we store kbd.next()
int charPosition = 0; //Our manual counter
for (int i = 0; i < mazeValue.length; i++) {
for (int j = 0; j < mazeValue[i].length; j++) {
if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
mazeValue[i][j] = 'X';
else {
while( input.equals("") || input.length() <= charPosition )
{
input = kbd.next();
charPosition = 0;
}
mazeValue[i][j] = input.charAt(charPosition);
charPosition++;
}
}
}
printMaze(mazeValue);
System.out.println("");
openfactor(mazeValue, n);
System.out.println("");
horizontalPath(mazeValue, n);
System.out.println(" ");
verticalPath(mazeValue,n);
System.out.println(" ");
openfactor(mazeValue, n);
average(mazeValue,n,totalOpenness);
}