gpt4 book ai didi

java - do-while 循环出现问题。使用 BlueJ 编译成功

转载 作者:太空宇宙 更新时间:2023-11-04 14:00:52 25 4
gpt4 key购买 nike

每个完美立方(例如 8、27、216 等)都可以表示为一系列连续奇数的总和。事实上,这样的序列将恰好包含“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)

以下代码生成一系列连续奇数,其总和等于作为输入的完美立方(变量名称“cube”)。 问题 - 在给定的代码中没有语法错误,并且尽管存在 do-while 循环,但它仅运行一次,这确保用户可以通过在询问时输入是/否 (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;
}
}

最佳答案

只需在 while 循环内添加一个新的扫描仪,它就可以工作:

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

输入 - 输出:

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

正如你所看到的,只要你输入“y”或“Y”它就会继续运行,当你输入时程序退出,如你所愿!

顺便说一句,您的代码无法编译。这是一个编译并成功运行的 Java 类。您的代码无法编译的原因是

1) 你没有正确声明类的main方法2)您没有创建该类的实例,因此您无法按照您尝试的方式调用该方法(这会导致编译错误)。

请参阅以下有效代码:

 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;
}}

关于java - do-while 循环出现问题。使用 BlueJ 编译成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29319810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com