In Java, you have to specify the data type of each variable as you declare it, like this:
intvideosWatched= 353;
Java has 8 primitive data types:
Data Type
Size
Description
byte
1 byte
Stores whole numbers from -128 to 127
short
2 bytes
Stores whole numbers from -32,768 to 32,767
int
4 bytes
Stores whole numbers from -2,147,483,648 to 2,147,483,647
long
8 bytes
Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float
4 bytes
Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double
8 bytes
Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean
1 bit
Stores true or false values
char
2 bytes
Stores a single character/letter or ASCII values
You can print variables and text to the console like this:
System.out.println("Number of videos watched: "+videosWatched);
๐ป Add each of the following variables to your code, and print each one to the console.
Be sure to use an appropriate data type.
// add a variable that stores your username
// add a variable that stores the number of videos youve uploaded
// add a variable that stores the average rating you give to videos
// add a variable that stores the theme setting as 'D''L' or 'A'for dark mode, light mode, or auto
// add a variable that stores whether you are signed in or not
// add a variable that stores the name of your country
Scannerscanner=newScanner(System.in);// create a Scanner object
System.out.print("Are you having a good class so far? (y/n)");Stringinput=scanner.nextLine();// get the user input
A big part of Wordle is the feedback from the game. After each guess, the user is shown their guess, and each letter is highlighted according to these rules:
GRAY backround: guess letters not included in the word
YELLOW backround: guess letters in the wrong location
GREEN backround: guess letters in the correct location
๐ป Each time the user guesses a word, print the word in the terminal, formatted with background colors.
Right now, the user will be asked for 6 guesses no matter what. However, if they guess correctly, the loop should end early. Here are three examples of while loops:
// java while loop
while(i< 6){System.out.println("Hello World");i++;}
// java while loop with OR logic
while(i< 6 ||i< 1){System.out.println("Hello World");i++;}
// java while loop with AND logic
while(i< 6 &&i> 0){System.out.println("Hello World");i++;}
๐ป Edit the loop so that if the user guesses correctly, it will end early.
// first some setup create variable yellowBackground, set to \u001b[47;1m create variable greenBackground, set to \u001b[42;1m create variable greyBackground, set to \u001b[47;1m create variable reset, set to \u001b[0m
print “Enter a 5-letter word” create variable found, set to false
// begin the guessing create variable i, set to 0 loop while i <= 6 and found is false
create variable input, set to the user’s input
// let’s check if they got it right if input is the same as word
print “Well done! You guessed the word!” set found to true
end if
// we need to format the output string correctly create variable output set to an empty string
//now we loop through each letter to format it correctly loop j from 0 to the length of the input
create variable wordLetter, set to the letter from the actual word// (use .charAt(j)) create variable guessLetter, set to the letter from the input//(use .charAt(j)) create variable formattedLetter, set to the guessLetter with the grey background
// should the letter be green? if the letters wordLetter and guessLetter match
set formattedLetter to the guessLetter with green background
// should the letter be yellow? else
loop h from 0 to length of the word create variable compareLetter, set to the letter from the word// (use .charAt(h))
if the compareLetter matches the guessLetter
set formattedLetter to the guessLetter with yellow background
end if
end loop
end if add the formattedLetter to the end of the output
end loop