java - trouble with do-while loop. compiled successfully using BlueJ -
every perfect cube (for eg. 8, 27, 216 etc) can expressed sum of series of consecutive odd numbers. fact such series contain 'n' consecutive odd numbers 'n' cube root of given cube.
example: 1) 8=3+5= 2^3 (2 odd numbers) 2) 125=21+23+25+27+29= 5^3 (5 odd numbers) 3) 1000=91+93+95+97+99+101+103+105+107+109= 10^3 (10 odd numbers)
the following code generates series of consecutive odd numbers sum equal perfect cube taken input (variable name 'cube'). problem-in given code there no syntax errors , runs once inspite of do-while loop ensures user can try different cubes entering yes/no (y/n) when asked.
import java.util.*; class cube { void main() { /*gives series of consecutive odd numbers sum equal input value. input cube of number less 1000*/ int i,odd,t,sum,cube,n; string c="y"; scanner sc=new scanner(system.in); { i=1; odd=1; t=2; sum=0; system.out.println("\nenter cube"); cube=sc.nextint(); //input stored in 'cube' n=cuberoot(cube); /*'n' cube root of 'cube'. if 'cube' not perfect cube n=0*/ while(i<=n) { sum+=odd; //consecutive odd numbers are added in sum if(sum==cube) //loop stops if sum=cube { break; } else if (i==n && sum!=cube) { i=1; //counter goes 1 sum=0; odd=i+t; //odd becomes next odd number after 1 , 1 after t+=2; } else { i++; odd+=2; } } if (n!=0) { //if 'cube' perfect cube n never 0 system.out.print("\n"+cube+" = "); for(i=odd-2*(n-1);i<=odd;i+=2) { if(i==odd) system.out.print(i+"\n\n"); else system.out.print(i + " + "); } system.out.println("\ntry again? (y/n)\n"); c=sc.nextline(); } } while(c.equals("y")||c.equals("y")); //if c "y" loop should run again doesnt } int cuberoot(int cube) { /*returns cube root of cube , returns 0 if invalid */ int i; for(i=1;i<=1000;i++) //n sholud less 1000 { if(i*i*i==cube) //if i^3 = cube n=i return i; } system.out.println("\ninvalid input.");//prints if cube not perfect cube return 0; } }
just add new scanner inside while loop, , works:
system.out.println("\ntry again? (y/n)\n"); scanner sc2=new scanner(system.in); c=sc2.nextline();
input - output :
c : y enter cube 1 1 = 1 try again? (y/n) y enter cube 2 invalid input. enter cube 3 invalid input. enter cube 4 invalid input. enter cube 5 invalid input. enter cube 6 invalid input. enter cube 7 invalid input. enter cube 8 8 = 1 2 try again? (y/n) n
as can see, long input "y" or "y" keeps on running, when input program exits, wanted do!
by way, code not compile. here java class compiles , runs successfully. reason code not compiled,
1) did not declare main method of class correctly 2) did not create instance of class, not call method way trying (it led compilation errors).
see following code works:
import java.util.*; class cube { public static void main(string[] args) { /*gives series of consecutive odd numbers sum equal input value. input cube of number less 1000*/ int i,odd,t,sum,cube,n; string c="y"; system.out.println("c : " + c); scanner sc=new scanner(system.in); { i=1; odd=1; t=2; sum=0; system.out.println("\nenter cube"); cube=sc.nextint(); //input stored in 'cube' cube mycube = new cube(); n=mycube.cuberoot(cube); /*'n' cube root of 'cube'. if 'cube' not perfect cube n=0*/ while(i<=n) { sum+=odd; //consecutive odd numbers are added in sum if(sum==cube) //loop stops if sum=cube { break; } else if(i==n && sum!=cube) { i=1; //counter goes 1 sum=0; odd=i+t; //odd becomes next odd number after 1 , 1 after t+=2; } else { i++; odd+=2; } } if(n!=0) //if 'cube' perfect cube n never 0 { system.out.print("\n"+cube+" = "); for(i=1;i<=n;i++,odd-=2) //i gives required odd numbers of series { system.out.println(i); } system.out.println("\ntry again? (y/n)\n"); scanner sc2=new scanner(system.in); c=sc2.nextline(); } } while(c.equals("y")||c.equals("y"));//if c "y" loop should run again doesnt } int cuberoot(int cube) { /*returns cube root of cube , returns 0 if invalid */ int i; for(i=1;i<=1000;i++)//n sholud less 1000 { if(i*i*i==cube) //if i^3 = cube n=i return i; } system.out.println("\ninvalid input.");//prints if cube not perfect cube return 0; }}
Comments
Post a Comment