java - Why i am getting this output while printing numbers numbers of a particular interval -
i trying print the numbers in between particular intervals taking starting, ending interval range of particular interval not able correct output.
i using scanner class take input user.
have used start1, start2 , end1, end2 of 2 intervals show starting , ending range.
import java.util.scanner; public class smallest234number { public static void smallest2nointerval(int p[],int x[]) { system.out.println("first interval"); for(int k=0;k<p.length;k++) { system.out.println(p[k++]); } system.out.println("second interval"); for(int k=0;k<x.length;k++) { system.out.println(x[k++]); } } public static void main(string[] args) { int ar1[]=new int[10]; int ar2[]=new int[10]; int i=0; int s=0; // have taken 2 intervals base system.out.println("enter first interval"); scanner sc=new scanner(system.in); system.out.println("enter first of starting interval"); int startno1=sc.nextint(); //now take input user startno system.out.println("enter end of starting interval"); int endno1=sc.nextint(); //now take input user endno system.out.println("enter second interval"); system.out.println("enter first of second interval"); int startno2=sc.nextint(); //now take input of 2interval user startno1 system.out.println("enter end of second interval"); int endno2=sc.nextint(); //now take input of 2interval user endno1 system.out.println("enter first interval nos between start no , end no"); system.out.println("enter first interval nos between start no , end no"); for(int a=startno1;a<=endno1;a++) { int inputno=sc.nextint(); if(inputno<=endno1&&inputno>=startno1) { ar1[s]=inputno; s++; } } system.out.println("enter second interval nos between start no1 , end no1"); for(int a=startno2;a<=endno2;a++) { int inputno=sc.nextint(); if(inputno<=endno1&&inputno>=startno1) { ar2[i]=inputno; i++; } } smallest2nointerval(ar1,ar2); } } **output shown:** enter first interval enter first of starting interval 1 enter end of starting interval 3 enter second interval enter first of second interval 2 enter end of second interval 4 enter first interval nos between start no , end no enter first interval nos between start no , end no 1 2 3 enter second interval nos between start no1 , end no1 2 3 4 first interval 1 3 0 0 0 second interval 2 0 0 0 0
you using same variable i
indexing in both arrays ar1
, ar2
that's why program puts integers on first 2 places in ar1
, 3rd place in ar2
. shall use different index variables or reset i=0
before putting values in ar2
.
edit problem in printing logic
for(int k=0;k<p.length;k++) { system.out.println(p[k++]); } system.out.println("second interval"); for(int k=0;k<x.length;k++) { system.out.println(x[k++]); }
you incrementing index variable twice in loop please correct this:
for(int k=0;k<p.length;k++) { system.out.println(p[k]); } system.out.println("second interval"); for(int k=0;k<x.length;k++) { system.out.println(x[k]); }
Comments
Post a Comment