This question deals with extending already existing Java code via a while loop.
Sample Code Structure
// add necessary import commands
public class main {
public static void main (String[] args){
// create the required variables first.
while (not all inputs have been taken - add this condition here){
//add the provided code.
}
}
}
Sample Output
Provided Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// creating object of Scanner class
Scanner sc = new Scanner(System.in);
// asking user to enter year
System.out.print("What year do you want to test? (Make sure it's between 1500 and 2019!)");
int year = sc.nextInt();// reading year
// checking year
if (year > 1500 && year < 2019) {
// checking leap year
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
// print message when year is leap year
System.out.println(year + " is a leap year!");
} else {
// print message when year is not leap year
System.out.println(year + " is NOT a leap year!");
}
} else {
// print message if invalid year is entered
System.out.println("This year cannot be checked. Try again!");
}
}
}
Source code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// creating object of Scanner class
Scanner sc = new Scanner(System.in);
//asking how many time the user wants to enter years
System.out.print("How many years do you want to check (must be between 1 and 5)?");
int count = sc.nextInt(); //reading number
while(count>0){ //counting how many times user entered
// asking user to enter year
System.out.print("What year do you want to test? (Make sure it's between 1500 and 2019!):");
int year = sc.nextInt();// reading year
// checking year
if (year > 1500 && year < 2019) {
// checking leap year
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
// print message when year is leap year
System.out.println(year + " is a leap year!");
} else {
// print message when year is not leap year
System.out.println(year + " is NOT a leap year!");
}
} else {
// print message if invalid year is entered
System.out.println("This year cannot be checked. Try again!");
}
count--; //decreasing the count value
}
}
}
Output:
This question deals with extending already existing Java code via a while loop. Ask the user...
Java Programming USE ONLY 1 CLASS. Class main is all that is required 1. Ask the user for a year as an input (positive integer - should be between 1500 and 2020, and can include 1500 and 2020). 2. If the year input is not between 1500 and 2020, do not check for leap year. Instead, print that this year cannot be checked. 3. If the input year provided by the user is a leap year, print that year is...
Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds,...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
(a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) { System.out.println ("Hello"); x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...
Complete the do-while loop to output 0 to countLimit. Assume the user will only input a positive number. import java.util.Scanner; public class CountToLimit { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int countLimit = 0; int printVal = 0; // Get user input countLimit = scnr.nextInt(); printVal = 0; do { System.out.print(printVal + " "); printVal = printVal + 1; } while ( /* Your solution goes here */ ); System.out.println(""); return; } }
Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with newline Ex: For the user input 123, 395, 25, the expected output is: Enter a number (<100): Enter a number (<100): Enter a number (<100): Your number < 100 is: 25 import java.util.Scanner; public class NumberPrompt { public static void main (String [] args) { Scanner scnr = new Scanner(System.in);...
(java) Write a While loop that prompts the user for only even numbers. Keep prompting the user to enter even numbers until the user enters an odd number. After the loop ends, print the sum of the numbers. Do not include that last odd number. You should not store the numbers in an array, just keep track of the sum. Make sure to use a break statement in your code. You may assume that a java.util.Scanner object named input has...
The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...
Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...