Java 8 | Scanner Class Practice

Student Kim
Buzz Code
Published in
2 min readJan 19, 2021

Hi guys! Welcome to another java practice session with me! Following from last time, let’s keep practicing the Scanner Class! If you missed the last post, the link is down below.

[Question 1] Build a program below.

So this program repeatedly asks if you want some more food, and when you type “yes” on the console it says “Here you go! :)”, and when you type “no” on the console, it says “Ok… :(” and finish the program. The answer is on below.

So I used for loop here, but there are something new about this for loop! As you see I didn’t put any initial value, condition, or the increment/decrement operators. Then it means this for loop is infinite, but we can end this with the break; when we need.

[Question 2] Build a program like below.

int[] numbers = new int[30];

Put the random numbers in the integer array numbers first, and make it say what number is in the index you typed.

The answer is on below.

I used the Random Class to generate the random numbers. And let the user type the number between 0–29 on the console. Then print the value out in that index.

Well that’s all for today guys, I bet it was pretty easy for all of you! Well done today guys see ya!

--

--