I am doing an exercise in the book "Java, How to Program." I have to make an application that simulates fraud. I have to make a method (flip) that accidentally returns the side for the coin. I decided to make the method return 1 or 2, and in the main method "convert" the values ββto one of the sides of the coin. The problem is that I get an error message that says: "Type mismatch -cannot convert from int to boolean". I really think that I only work with integers, and I donβt see how logical lines arise.
The code is as follows:
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; } }
source share