使用while循环声明一个不可能的输出

时间:2013-09-14 17:46:14

标签: java

import java.io.*;
import java.util.*;

public class volumeConeD

{//class
    public static void main (String [] args)
    {//main

        Scanner keyBoard = new Scanner(System.in);//input for keyBoard
        //variables
        double volume;
        double radius;
        double hieght;
        double pie = 3.14;
        double yes = 1.0;
        boolean volumeTwo = true;


        while(volumeTwo == 0){
            System.out.print("Volume of a Cone... V=1/3(3.14)r^2(h)");
            System.out.println ();
            System.out.println ();

            radius = getRadius(radius); //call to method
            System.out.print("Enter a Height      ");
            hieght = keyBoard.nextDouble ();
            //math
            volume = .33333 * pie * radius * radius * hieght;
            System.out.printf ("Volume =       " + volume);

        }//end of while         
    }//end of main
    public static double getRadius (double radius)
    {
        Scanner keyBoard = new Scanner(System.in);

        System.out.print("Enter Radius Squared Number      "); 
        radius = keyBoard.nextDouble ();
        return radius;
    }
}//end of program

所以这是我的问题。我必须这样写,如果答案最终是Volume = 0,程序必须结束。我必须使用while循环和该方法来输入半径。我一直收到这个错误,我无法弄清楚原因。

               error
                       volumeConeD.java:25: error: incomparable types: boolean and int
while(volumeTwo == 0){
                ^
1 error.

我理解错误意味着什么,但我无法弄清楚如何解决它。请帮忙 NEW EDIT ...也必须在while循环中读取,而(Volume == 0)。

4 个答案:

答案 0 :(得分:1)

如果您希望在while(volumeTwo) volumeTwo truewhile(!volumeTwo)期间继续volumeTwo,请false {{1}} {{1}} {{1}} }。

答案 1 :(得分:0)

您尝试将boolean0进行比较,嗯

boolean volumeTwo = true;
while(volumeTwo == 0)

使用

 while(volumeTwo == true)

while(volumeTwo)  

答案 2 :(得分:0)

完全摆脱volumeTwo。您关心volume是否为0,因此只需将while循环更改为while(volume!=0)并确保将卷初始化为0以外的其他内容。

答案 3 :(得分:-1)

我认为以下是您要实现的目标。请参阅标有< =======:

的评论
    ... 
    //boolean volumeTwo = true; // <======= Manoj - COMMENT THIS LINE  
    double volumeTwo = 1.0; // <=========== Manoj - any non-zero for that matter
    ... 

    ... 
    while(volumeTwo != 0.0){
    ... 
              //math
      volume = .33333 * pie * radius * radius * hieght;
      System.out.printf ("Volume =       " + volume);
      volumeTwo = volume; // <=============== Manoj - update volumeTwo with calculated volume 
                          // <===============       - when volumeTwo becomes 0.0 loop quits