Jamiemcg

Yahoo! Answers Question Response - Java Scanner and Output

Jul 20th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. /* The purpose of this class is to get five user inputs and print them to the screen, using the
  2.  * Scanner class, as well as some variables and a for loop.
  3.  * */
  4.  
  5. //import the Scanner class (which is in the "java.util" library) so that we can get user input
  6. import java.util.Scanner;
  7.  
  8. public class YahooAnswers{
  9.  
  10.   //main method of the YahooAnswers class (where we will write most of the code in this class)
  11.   public static void main(String [] args){
  12.  
  13.     //create 2 variables, one for the current input value and one for the total of all input values
  14.     //added together.
  15.     int userInput = 0;
  16.     int total = 0;
  17.    
  18.     //create an object of the Scanner class, called "input", to get users' input
  19.     Scanner input = new Scanner(System.in);
  20.    
  21.     //create a for loop to get 5 int values from the user, using the "input" object of the Scanner class
  22.     for(int i = 0; i <= 5; i++){
  23.      
  24.       //prompt the user to enter a value
  25.       System.out.println("Please enter an integer!");
  26.      
  27.       //scan for user input (using the "nextInt()" method of the Scanner class)
  28.       //and store it in the "userInput" variable
  29.       userInput = input.nextInt();
  30.      
  31.       //add the user-entered values together every time the loop runs
  32.       total += userInput;
  33.      
  34.       //print the value the user just entered
  35.       System.out.println("You just entered: " + userInput);
  36.      
  37.       //print the total value
  38.       System.out.println("Total: " + total);      
  39.      
  40.     }
  41.    
  42.   }
  43.  
  44. }
Add Comment
Please, Sign In to add comment