Ex2InputInteger

Ex2InputInteger - Example 2: Input, Numbers: Getting user...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 2: Input, Numbers: Getting user input and adding numbers /* class ConvertTemperature * * Purpose: show how to read information from user, use numbers */ 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[]) { // Tell Java that we want some numbers (integers) to use // These are called variables . Here, we declare our variables. int celsius; int fahrenheit; Scanner input; // this is to read from keyboard // create Scanner so that we can read from the keyboard input = new Scanner( System.in ); // print a message to the screen System.out.println(“Enter the number you want to convert to F”); // the scanner reads the user’s value. This places the value // into the “celsius” variable celsius = input.nextInt(); // calculate the result fahrenheit = ((celsius * 9) / 5) + 32; // print out the result
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

Ex2InputInteger - Example 2: Input, Numbers: Getting user...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online