简体   繁体   English

while循环语句出现问题

[英]Trouble with while-loop statement

I keep getting errors when I try to compile these codes below, I'm currently using JCreator. 当我尝试在下面编译这些代码时,我不断收到错误消息,我目前正在使用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

Error: 错误:

--------------------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.

You can't use the = operator to compare values; 您不能使用=运算符比较值; that's the assignment operator. 那是赋值运算符。 Use == to compare your int values: 使用==比较您的int值:

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

The assignment operator here evaluates to an int , yielding the error message that you received. 赋值运算符在这里计算为int ,产生您收到的错误消息。

counter = 0

应该

counter == 0

while ((counter = 0)&&(number > 0)) is never true, because (counter = 0) assigns counter to be 0 and the value of that statement is the value of counter : 0 . while ((counter = 0)&&(number > 0))永远不会为真,因为(counter = 0)将counter分配为0并且该语句的值是counter的值: 0 And 0 is int and can't be converted to boolean . 而且0int ,不能转换为boolean

Where is the question? 问题在哪里?

There are so many errors in this code... To check if two integers are equal you have to write == not =. 该代码中有太多错误...要检查两个整数是否相等,必须编写== not =。 (In while and in if). (在while和if中)。

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

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