[英]Given an array of integers check whether there are two numbers present with given product
http://www.practice.geeksforgeeks.org/problem-page.php?pid=667 You can find Qusetion on above link .Why it is not showing me the required output. http://www.practice.geeksforgeeks.org/problem-page.php?pid=667你可以在上面的链接上找到 Qusetion。为什么它没有显示我所需的输出。 Infact I am just putting value of N which is array size, P = required output arrays values in array but after taking all values it is not showing any result.事实上,我只是将 N 的值放入数组大小,P = 数组中所需的输出数组值,但在获取所有值后,它没有显示任何结果。
package practice;
import java.util.Scanner;
public class Practice {
public static void main(String a[])
{
Scanner in =new Scanner(System.in);
int N = in.nextInt();
int[] arr = new int[N];
int P = in.nextInt();
for(int i =0 ; i<=N ; i++){
arr[i] = in.nextInt();
}
for(int j=0 ; j<arr.length;j++){
for(int k = j+1;k>=j;++k){
if(arr[j]*arr[k]==P){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
}
}
Run Time Error Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at GFG.main(File.java:14)线程“main”中的运行时错误异常 java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner .nextInt(Scanner.java:2160) 在 java.util.Scanner.nextInt(Scanner.java:2119) 在 GFG.main(File.java:14)
Instead of this for loop而不是这个 for 循环
for(int k = j+1;k<=j;++k)
you should have this:你应该有这个:
for(int k = j+1;k< arr.length;++k)
in your version you start k
from j+1
which is by definition larger that j
hence you never get to the inside of that loop.在您的版本中,您从j+1
开始k
,根据定义, j
大,因此您永远不会进入该循环的内部。
And also this line:还有这一行:
for(int i =0 ; i<=N ; i++)
Should be this:应该是这样的:
for(int i =0 ; i< N ; i++)
That's because you are creating an array of size N
but setting its values for N+1
times.那是因为您正在创建一个大小为N
的数组,但将其值设置为N+1
次。
for(int k = j+1;k<=j;++k){ for(int k = j+1;k<=j;++k){
if J is 10, then K starts at 11 which automatically breaks the condition K <=J, and the inner loop never runs.如果 J 是 10,则 K 从 11 开始,这会自动打破条件 K <=J,并且内循环永远不会运行。
Your runtime error is being thrown by the Scanner.扫描仪正在抛出您的运行时错误。 Sometimes a Java Scanner can pick up a newline character, which throws a runtime exception.有时,Java Scanner 可以拾取换行符,这会引发运行时异常。 Try using a BufferedReader : BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
尝试使用BufferedReader : BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Also, hen you are filling the array, remember that arrays go from index 0, to array.length-1
.另外,在填充数组时,请记住数组从索引 0 到array.length-1
。 When you create an array like: int[] arr = new int[N];
当你创建一个数组时: int[] arr = new int[N];
, you get a new int
array of N indices. ,你会得到一个新的 N 个索引的int
数组。 Because the first index is at 0, the final index is at N-1
.因为第一个索引在 0 处,所以最后一个索引在N-1
。 Therefore, in the for
loop when k = N
, the index arr[i]
is 1 greater than the largest index.因此,在for
循环中,当k = N
,索引arr[i]
比最大索引大 1。 Using for(int i = 0; i < N; i++){...}
will prevent that.使用for(int i = 0; i < N; i++){...}
将阻止这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.