简体   繁体   English

尽管未使用布尔值,但我收到“类型不匹配无法从int转换为布尔值”

[英]I get “Type mismatch cannot convert from int to boolean” despite not using boolean

I am doing an excercise in the book "Java how to program". 我正在“ Java如何编程”一书中做练习。 I am supposed to make an application that simulates coin-tossing. 我应该做一个模拟投币的应用程序。 I am supposed to make a method (flip) which returns randomly a side for the coin. 我应该做一个方法(翻转),随机返回硬币的一面。 I have desided to make the method return either 1 or 2, and in the main-method I "convert" the values to mean one of the sides of the coin. 我倾向于使该方法返回1或2,在主方法中,我将值“转换”为代币一侧。 The problem is that I get an error message that says: "Type mismatch -cannot convert from int to boolean". 问题是我收到一条错误消息,内容为:“类型不匹配-无法从int转换为boolean”。 I really think that I am operating only with integers all the way, and canot see how the booleans come in. 我真的认为我一直都只使用整数,并且不知道布尔值是如何出现的。

The code is the following: 代码如下:

import java.util.Random;

public class Oppgave629 
{

    public static void main(String[] args) 
    {
        int command = 1;
        int heads = 0;
        int tails = 0;
        while (command != -1)
        {
            System.out.print("Press 1 to toss coin, -1 to exit:");
            int coinValue = flip();
            if (coinValue = 1) {System.out.println("HEADS!"); heads++;}
            if (coinValue = 2) {System.out.println("TAILS!"); tails++;}
            System.out.printf("Heads: %d", heads); System.out.printf("Tails: %d", tails);
        }
    }

    static int flip()
    {
        int coinValue;
        Random randomValue = new Random();
        coinValue = 1 + randomValue.nextInt(2);
        return coinValue;
    }
}

Your code 您的密码

if (coinValue = 1) {System.out.println("HEADS!"); heads++;}
if (coinValue = 2) {System.out.println("TAILS!"); tails++;}

Should be 应该

if (coinValue == 1) {System.out.println("HEADS!"); heads++;}
if (coinValue == 2) {System.out.println("TAILS!"); tails++;}

You're assigning an int type to coinValue and that is being evaluated as a bool inside the if statement. 您正在为intValue分配一个int类型,并且该类型在if语句中被作为布尔值进行评估。

You're using an assignment (=) operator instead of a comparison operator (equality ==) in your if statements: 您在if语句中使用赋值(=)运算符,而不是比较运算符(等于==):

if (coinValue = 1)

Should be 应该

if (coinValue == 1)

An if statement expects a boolean expression, you're assigning 1 to coinValue and it's then trying to interpret this as a boolean to process the conditional. 如果if语句期望一个布尔表达式, coinValue 1分配给coinValue ,然后尝试将其解释为布尔值以处理条件。

This one is part of the most common and dangerous programming errors developers do. 这是开发人员最常见和最危险的编程错误的一部分。 In the old days, compilers (such as C compiler) won't complain about if(coinValue = 1) statement as it will affect coinValue to 1 and always evaluate the condition to true as it equals 1 Fortunately, the Java compiler catches this error and does not let you do this. 在过去,编译器(例如C编译器)不会抱怨if(coinValue = 1)语句,因为它将影响coinValue到1并始终将条件评估为true等于1,幸运的是,Java编译器捕获了此错误并且不允许您这样做。 As stated in the answers above, change if (coinValue = 1) to if (coinValue == 1) and your problem should be fixed. 如以上答案中所述,将if (coinValue = 1)更改为if (coinValue == 1) ,您的问题应得到解决。

I ran into this error b/ci had i=4 in a for statement ie "for (i=1;i=4;i++)...." 我遇到了这个错误b / ci在for语句中有i = 4,即“ for(i = 1; i = 4; i ++)....”

you can't use an equal to "=" in the for command for the stop value. 您不能在for命令中使用等于“ =”作为停止值。

if (coinValue == 1) {System.out.println("HEADS!"); heads++;} 
if (coinValue == 2) {System.out.println("TAILS!"); tails++;}

instead of 代替

 if (coinValue = 1) {System.out.println("HEADS!"); heads++;}    
 if (coinValue = 2) {System.out.println("TAILS!"); tails++;}    

with the 2 equal signs 两个等号

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

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