ex2b - celsius = input.nextInt(); // calculate the result...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 2b: Getting user input /* class ConvertTemperature * * Purpose: show how to read information from the user */ import java.util.Scanner; // use the Java built-in class Scanner public class ConvertTemperature { /* main * * The main method begins any Java program * This method prints a message to the screen. */ public static void main(String[] args) { // declare two variables int celsius; int fahrenheit; // declare a third variable for reading from keyboard Scanner input; // create & initialize Scanner input = new Scanner( System.in ); // print a message to the screen System.out.println("Enter the temp you want to convert to F"); // the scanner reads the user’s value. This places the value // into the "celsius" variable
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: celsius = input.nextInt(); // calculate the result fahrenheit = ((celsius * 9) / 5) + 32; // print out the result System.out.printf("%d degrees Celsius = %d degrees Fahrenheit\n", celsius, fahrenheit); } // end of main } // end of class ConvertTemperature System.in - the name that Java gives to input received from the keyboard Scanner - a class provided by Java to help read information from the keyboard or files nextInt(); - the method to read a number from the keyboard // note how the variable name is used in these three spots. Scanner x; x = new Scanner(System.in); x.nextInt(); Questions about above code: What happens if we replace celsius = input.nextInt(); with input.nextInt();...
View Full Document

This note was uploaded on 12/27/2011 for the course CMPSC 5 taught by Professor Franklin during the Fall '09 term at UCSB.

Ask a homework question - tutors are online