In: Computer Science
ASAP PLEASE!!!! USING JAVA
/*
1. When should you use a do-while loop?
** Write your answer as a multi-line Java comment **
*/
/*
2. Identify the algorithm that matches this code snippet. Your choices are:
sum and average, counting matches, first match, prompt until match, and
comparing adjacent values. Write your answer below the coded.
int firstNum = 0;
int number = scnr.nextInt();
while (scnr.hasNextInt())
{
int input = scnr.nextInt();
if (input == number)
{
firstNum++;
}
}
My answer is:
*/
/*
3.
Write a Java code snippet with a do-while loop to validate user input. Prompt the
user to enter a value less than 100. If the user doesn't enter a value less than 100,
ask again until they provide a valid number. Print the valid number to the console.
*/
/*
4. Write a Java code snippet with a nested for loop to
print five rows of six random integers between 5 and 10 inclusive.
*/
/*
5. Currency conversion: Write a snippet that first asks the user to type
today's US dollar price for one Euro. Then use a loop to:
-- prompt the user to enter a Euro amount. (allow decimals)
-- convert that amount to US dollars. (allow decimals)
-- print the amount to the screen, formatted to two decimal places
Use 0 as a sentinel to stop the loop.
*/
Please ask 1 question at a time. I have done first 3
1
We should use a do while loop when we need the loop to run atleast
once. For examle , while validating user input, we can use it, as
we need the loop to run atleast once, for user to input
2)The algorithm is counting matches
We first take an integer input. Then we take more integer inputs,
and increase counter if any input matches the first
integer.
3)
int num;
do{
System.out.println("Enter a value less than 100: ");
num = sc.nextInt();
}while(num>=100);
System.out.println("You entered: " + num);