简体   繁体   English

做循环的麻烦。 使用BlueJ成功编译

[英]trouble with do-while loop. compiled successfully using BlueJ

Every perfect cube (for eg. 8, 27, 216 etc) can be expressed as the sum of a series of consecutive odd numbers. 每个完美的立方(例如8、27、216等)都可以表示为一系列连续奇数的总和。 It is a fact that such a series will contain exactly 'n' consecutive odd numbers where 'n' is the cube root of the given cube. 事实是这样的序列将精确地包含“ n”个连续的奇数,其中“ n”是给定立方体的立方体根。

 For 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 a series of consecutive odd numbers whose sum is equal to a perfect cube which is taken as input (variable name 'cube'). 下面的代码生成一系列连续的奇数,这些和的总和等于一个作为输入的变量的完美立方(变量名'cube')。 PROBLEM-In the given code there are NO syntax errors and it runs only once inspite of a do-while loop which ensures that the user can try different cubes by entering Yes/No (Y/N) when asked. 问题-在给定的代码中没有语法错误,尽管有do-while循环,但它仅运行一次,从而确保用户可以通过在询问时输入Yes / No(Y / N)来尝试不同的多维数据集。

import java.util.*;

class CUBE {   

    void main() {              
        /*gives a series of consecutive odd numbers                                          
        whose sum is equal to the input value.  
        input is a cube of a number less than 1000*/

        int i,odd,t,sum,cube,n; String c="y"; 
        Scanner sc=new Scanner(System.in);        

        do {
            i=1;
            odd=1;
            t=2;
            sum=0;
            System.out.println("\nEnter the cube");
            cube=sc.nextInt(); //input is stored in 'cube'
            n=cubeRoot(cube);  /*'n' is the cube root of 'cube'. If 'cube'  
                                     is not a perfect cube then n=0*/
            while(i<=n) {
                sum+=odd;  //consecutive odd numbers are are added in sum    
                if(sum==cube) //loop stops if sum=cube
                {
                    break;
                }
                else if (i==n && sum!=cube) {
                    i=1; //counter goes back to 1
                    sum=0;
                    odd=i+t; //odd becomes the next odd number just after 1 and then the one after that
                    t+=2;
                }
                else {
                    i++; 
                    odd+=2;
                }
            }
            if (n!=0) { //if 'cube' is a perfect cube then n will never be 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 is "y" then loop should run again but it doesnt
    }

    int cubeRoot(int cube)  {                    
        /*returns the cube root of 
                    cube and returns 0 if its 
                    invalid */

        int i;
        for(i=1;i<=1000;i++) //n sholud be less than 1000
        {
            if(i*i*i==cube) //if i^3 = cube then n=i
                return i;
        }
        System.out.println("\nINVALID INPUT.");//prints if cube is not a perfect cube 
        return 0;
    }
} 

just add a new scanner inside the while loop, and it works: 只需在while循环内添加一个新的扫描仪,它就可以工作:

                   System.out.println("\nTry again? (Y/N)\n"); 
                   Scanner sc2=new Scanner(System.in);                                 
                   c=sc2.nextLine();

INPUT - OUTPUT : 输入输出 :

c is : y

Enter the cube
1

1 = 1

Try again? (Y/N)

Y

Enter the cube
2

INVALID INPUT.

Enter the cube
3

INVALID INPUT.

Enter the cube
4

INVALID INPUT.

Enter the cube
5

INVALID INPUT.

Enter the cube
6

INVALID INPUT.

Enter the cube
7

INVALID INPUT.

Enter the cube
8

8 = 1
2

Try again? (Y/N)

N

as you can see, as long as you input "y" or "Y" it keeps on running, when you input the program exits, as you wanted to do! 如您所见,只要您输入“ y”或“ Y”,它就会继续运行,当您输入程序退出时,就可以这样做!

By the way, your code does not compile. 顺便说一下,您的代码无法编译。 Here is a Java class which compiles and runs successfully. 这是一个Java类,可成功编译并运行。 The reason your code could not be compiled, was that 您的代码无法编译的原因是

1) you did not declare the main method of the class correctly 2) you did not create an instance of the class, so you could not call the method the way you were trying to (it led to compilation errors). 1)您没有正确声明该类的主要方法2)您没有创建该类的实例,因此您无法以您尝试的方式调用该方法(这会导致编译错误)。

See the following code which works: 请参阅以下有效的代码:

 import java.util.*;
         class CUBE {   

        public static void main(String[] args)   {              /*gives a series of consecutive odd numbers                                          
                                       whose sum is equal to the input value.  
                                       input is a cube of a number less than 1000*/

                int i,odd,t,sum,cube,n; 
                String c="y"; 
                System.out.println("c is : " + c);
                Scanner sc=new Scanner(System.in);        

                do
                {
                    i=1;
                    odd=1;
                    t=2;
                    sum=0;
                    System.out.println("\nEnter the cube");
                    cube=sc.nextInt(); //input is stored in 'cube'
                    CUBE myCube = new CUBE();
                    n=myCube.cubeRoot(cube);  /*'n' is the cube root of 'cube'. If 'cube'  
                                         is not a perfect cube then n=0*/


                  while(i<=n) 

                    {
                        sum+=odd;  //consecutive odd numbers are are added in sum    
                       if(sum==cube) //loop stops if sum=cube
                        {
                            break;
                        }
                        else if(i==n && sum!=cube)
                        {
                            i=1; //counter goes back to 1
                            sum=0;
                            odd=i+t; //odd becomes the next odd number just after 1 and then the one after that
                            t+=2;
                        }
                        else
                        {
                            i++; 
                            odd+=2;
                        }
                    }
                    if(n!=0) //if 'cube' is a perfect cube then n will never be 0
                    {
                       System.out.print("\n"+cube+" = ");
                       for(i=1;i<=n;i++,odd-=2) //i gives the required odd numbers of the 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 is "y" then loop should run again but it doesnt
            }


            int cubeRoot(int cube)  {                    /*returns the cube root of 
                                                          cube and returns 0 if its 
                                                          invalid */

                int i;
                for(i=1;i<=1000;i++)//n sholud be less than 1000
                {
                    if(i*i*i==cube) //if i^3 = cube then n=i
                      return i;
                }
                System.out.println("\nINVALID INPUT.");//prints if cube is not a perfect cube 
                return 0;
            }} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM