Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The purpose of this class is to get five user inputs and print them to the screen, using the
- * Scanner class, as well as some variables and a for loop.
- * */
- //import the Scanner class (which is in the "java.util" library) so that we can get user input
- import java.util.Scanner;
- public class YahooAnswers{
- //main method of the YahooAnswers class (where we will write most of the code in this class)
- public static void main(String [] args){
- //create 2 variables, one for the current input value and one for the total of all input values
- //added together.
- int userInput = 0;
- int total = 0;
- //create an object of the Scanner class, called "input", to get users' input
- Scanner input = new Scanner(System.in);
- //create a for loop to get 5 int values from the user, using the "input" object of the Scanner class
- for(int i = 0; i <= 5; i++){
- //prompt the user to enter a value
- System.out.println("Please enter an integer!");
- //scan for user input (using the "nextInt()" method of the Scanner class)
- //and store it in the "userInput" variable
- userInput = input.nextInt();
- //add the user-entered values together every time the loop runs
- total += userInput;
- //print the value the user just entered
- System.out.println("You just entered: " + userInput);
- //print the total value
- System.out.println("Total: " + total);
- }
- }
- }
Add Comment
Please, Sign In to add comment