0

当我尝试编译下面的这些代码时,我不断收到错误,我目前正在使用 JCreator。

import java.io.*;

public class Number //class name here, same as file name

 {
 public Number()throws IOException{//constructor, place class name here
 // use BufferedReader class to input from the keyboard
 // declare a variable of type BufferedReader
 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
 //declare variable for input
 String inputString;

int number;
int counter;
int square;
int cube;
String goodMessage = "Thank you";
String badMessage = "Sorry";

//begin houseKeeping()
System.out.print("Please input number: ");
inputString = input.readLine();
number = Integer.parseInt(inputString);

//begin squareCube()
counter = 0;
while ((counter = 0)&&(number > 0)) {
    square = number*number;
    cube = number*number*number;
    System.out.print(square);
    System.out.print(cube);
}
if (counter = counter + 1);
if (counter < 3);
System.out.print("Enter input number: ");

//begin finishUp()
if (number > 0)
    System.out.println(goodMessage);

    else 
    System.out.println(badMessage);

 }//end constructor

 public static void main(String [] args) throws IOException // main method

 {
 new Number(); //class constructor name
 } // end the main method
 } // end the program

错误:

--------------------Configuration: <Default>--------------------
D:\INFO\INFO 1391\Number.java:27: error: bad operand types for binary operator '&&'
    while ((counter = 0)&&(number > 0)) {
                        ^
  first type:  int
  second type: boolean
1 error

Process completed.
4

4 回答 4

2

您不能使用=运算符来比较值;那是赋值运算符。用于==比较您的int值:

while ((counter == 0)&&(number > 0)) {

此处的赋值运算符计算结果为int,产生您收到的错误消息。

于 2013-05-06T19:23:01.057 回答
2
counter = 0

应该

counter == 0
于 2013-05-06T19:23:28.353 回答
0

while ((counter = 0)&&(number > 0))永远不会正确,因为(counter = 0)将 counter 分配为 be0并且该语句的值是counter:的值0。并且0int并且不能转换为boolean.

于 2013-05-06T19:25:02.653 回答
0

问题在哪里?

这段代码中有很多错误...要检查两个整数是否相等,您必须编写 == not =。(在while和if中)。

于 2013-05-06T19:25:11.923 回答