Learn Java Tutorial 1.22- Finish our App with Boolean Variables

preview_player
Показать описание
In this java tutorial we will learn about the boolean data type, and how we can use it to do some error checking, if something is true, or for pretty much anything we want it to do. We will also finish creating our basic "game" in this tutorial.

Hit us up on G+, facebook, or twitter!

Follow us!
-------------
-------------

------------

Рекомендации по теме
Комментарии
Автор

Great tuts Travis. Greetings from Poand.

pampal
Автор

@BurakAIC the r.nextInt(100) will return a random number between 0 and 99 (which is 100 places), however we want a random number 1 through 100, so we add a +1 so we still are cycling through 100 choses (r.nextInt(100)) but now we getting a number 1-100 instead of 0-99.

mybringback
Автор

@natsud2 haha yea, there's one more after this one. I think you could tell I was quite tired in that video.. lol

mybringback
Автор

As I believe someone else mentioned, if you get even one number that isn't divisible by 2 then you lose. You would need 100% of the numbers to be divisible by 2 to win. If even one number is not divisible then the esle statement changes the gameResult to false and you lose. If you wanted to win based on the majority of numbers being divisible by 2 then you could add a variable that counts the number of wins and loses and then compare them afterwards.

Siphex
Автор

you could use an etiquette and break to be more efficient.I also believe it is possible that more than one number be the same.

madebyou
Автор

To make the code a bit better I would add a break after the gameResult = false. This would be better for people who are new to the "game" and so you do not have to sit around and wait for the results!

iansgametime
Автор

Implemented a restart function too, with boolean and userinput

YuZhengwen
Автор

@scamexdotexe sorry buddy, i have noooo idea. I would guess yes, but you never know.

mybringback
Автор

code part 1: import java.util.Random;


public class StartingPoint {public static void main(String[] args) {
Random r = new Random();int[] num = new int[5];boolean gameResult = true;for(int i = 0; i < num.length; i++){num[i] = r.nextInt(100) + 1;if (num[i] % 2 ==0){System.out.println("TRUE - " + num[i] + " is divisible by 2 - " + num[i] + " isn't divisible by 2 ");gameResult = false;}}

MrPSH
Автор

wow another one arent you gettig tired ? lol nice work

DustanWebb
Автор

Great tutorial!!!
I sort of got the game to work...
I ran it it showed two trues and it still said i lost... Any ideas?

Grosskev
Автор

if a boolean is false does it always do the else statement? @ 4:20 how does the if statement know what to do with gameResult?

franchyze
Автор

Is there a missing block of code?
The gameResult variable only has meaning on the outcome of the game the last time through the loop.
I think the number of true/falses should be stored and then tested against each others values.

Sentinel
Автор

Hey kdog0011 - same (sort of) happens to me when I run 5 times - I got 4 'false - [num] is not divisible by 2' and 1 'True - [num] is divisble by 2' followed by a 'You won!'...
My code is utterly identical to Travis' lol.
Feels like I'm so close!!

enjoytheflames
Автор

hey bro, can u code this for eg.there's an array of random numbers of 10size.You've to bring all the zero's to right side and sorted elements to the left side of an array?this was a question i got in an interview

sanketkhed
Автор

understand everything. but still i dont know why we write +1 after " r.nextInt(100) " ?

Bubu
Автор

@mybringback so now we have '100' but this time we can't use '0'.
thanks.

Bubu
Автор

code part 2: if HAVE have lost... :O");}}}

MrPSH
Автор

This is in regards to the second if/else statement in the code, my code only works if i use a double equals to check the Boolean value of gameResult. If I do not check it the program runs and always tells me I've won, even with numbers non divisible by two. Any ideas as to why this is happening?

sebastiangheorge
Автор

but there are times i get an even number but i still don't win why is that?

MrPSH
welcome to shbcf.ru