Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:53:08+00:00 2026-05-27T13:53:08+00:00

I am doing an excercise in the book Java how to program. I am

  • 0

I am doing an excercise in the book “Java how to program”. 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. The problem is that I get an error message that says: “Type mismatch -cannot convert from int to 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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:53:09+00:00Added an answer on May 27, 2026 at 1:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a book exercise that says to write a program that generates psuedorandom
I am doing excercises in a book called Java, how to program. I have
I am learning Java, now working with arrays. I am doing an excercise where
I'm doing the first chapter exercises on my Java book and I have been
I am a beginner in Java and am doing an exercise from a book.
i'm doing some exercises in my Java book. I'm very new to programming. Therefore,
i am doing an exercise from Orielly book, and they used that Twitter package
I am learning java, using the book Java how to program. I am solving
I am doing a book exercise (not homework, since i'm self-learning) in which I'm
I'm doing an exercise. The goal is to make a program in C to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.