[英]Printing an array in a while loop
I am trying to print my array using a method call once while the user inputs y or Y. I do not know how to get the array to only print once.我试图在用户输入 y 或 Y 时使用一次方法调用打印我的数组。我不知道如何让数组只打印一次。
I've researched and tried tweaking my own code but I am lost.我已经研究并尝试调整我自己的代码,但我迷路了。
public class Lab10p1 {
public static final int ROW = 5;
public static final int COL = 6;
public static final int MIN = 0;
public static final int MAX = 100;
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Do you want to start Y/N?");
char c =scan.next().charAt(0);
while(c=='y'||c=='Y')
{
int[][] a = new int[ROW][COL];
randArray(a, ROW, COL, MIN, MAX);
}
}
public static void randArray(int[][] matrix, int row, int col, int low, int up)
{
Random rand = new Random();
for (int r = 0; r < row; r++)
{
for (int c = 0; c < col; c++)
{
int are=matrix[r][c] = rand.nextInt(up - low + 1) + low;
System.out.print(" "+are);
}
System.out.println();
}
}
the expected output is预期的输出是
Do you want to continue(Y/N): y你想继续吗(是/否):是
The array elements are:数组元素为:
12 31 12 21 45 23 12 31 12 21 45 23
32 12 67 54 35 67 32 12 67 54 35 67
34 54 33 34 53 34 34 54 33 34 53 34
23 34 43 23 45 78 23 34 43 23 45 78
23 54 89 76 54 34 23 54 89 76 54 34
The actual results are an infinite loop of printing 6x5 arrays实际结果是无限循环打印6x5数组
You should use if
instead of while
.您应该使用
if
而不是while
。 If the user enters Y
or y
, the loop will never finish because c
is never set to anything else but Y
or y
inside the loop and therefore the loop condition is always true.如果用户输入
Y
或y
,则循环将永远不会完成,因为c
在循环内从未设置为Y
或y
任何其他值,因此循环条件始终为真。
If you want to ask the user again after each printed array, you have to include the question into the loop, but in this case I would prefer a do while
loop.如果您想在每个打印的数组之后再次询问用户,您必须将问题包含在循环中,但在这种情况下,我更喜欢
do while
循环。
Once you print the array
.一旦你打印了
array
。 prompt user to input character again so if he/she inputs Y again the print array again like:提示用户再次输入字符,因此如果他/她再次输入 Y,则再次打印数组,例如:
while(c=='y'||c=='Y')
{
int[][] a = new int[ROW][COL];
randArray(a, ROW, COL, MIN, MAX);
System.out.println("Do you want to start Again Y/N?");
c = scan.next().charAt(0);
}
Or if you want your program to run only once then replace while
with if
.或者,如果您希望程序只运行一次,则将
while
替换为if
。
That is happening because you input the value of 'c' only once and keep iterating it, now it goes into infinite loop because the value of 'c' doesn't change at all.这是因为您只输入了 'c' 的值一次并不断迭代它,现在它进入无限循环,因为 'c' 的值根本没有改变。
char c =scan.next().charAt(0);
int[][] a;
while(c=='y'||c=='Y')
{
a = new int[ROW][COL];
randArray(a, ROW, COL, MIN, MAX);
c =scan.next().charAt(0);
}
Also, you don't have to redeclare the 2D array each time.此外,您不必每次都重新声明二维数组。
two ways两种方式
if(c=='y'||c=='Y')
{
int[][] a = new int[ROW][COL];
randArray(a, ROW, COL, MIN, MAX);
}
or或者
while(c=='y'||c=='Y')
{
int[][] a = new int[ROW][COL];
randArray(a, ROW, COL, MIN, MAX);
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.