import java.util.Scanner;
class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in);
int x, y, ans;
System.out.print("Enter 1st number: ");
x = eggs.nextInt();
System.out.print("Enter 2nd number: ");
y = eggs.nextInt();
System.out.print("Answer: ");
ans = x + y;
System.out.println(ans);
}
}
Quick Links to Application questions (with solutions)!
Sunday, January 17, 2010
Words - Sentence (with user input) code
import java.util.Scanner;
class bananas
{
public static void main(String args[])
{
class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in); //eggs is just a word I picked.
System.out.println("Type words and system will show you what you typed in another line.");
System.out.println(eggs.nextLine()); //System gets eggs(scanner) and ensures they are words. (1) Eggs gets the input.[in] (2) System prints it out.[out]
System.out.println("Type words and system will show you what you typed in another line.");
System.out.println(eggs.nextLine()); //System gets eggs(scanner) and ensures they are words. (1) Eggs gets the input.[in] (2) System prints it out.[out]
System.out.println("Type numbers and system will show you what you typed in another line.");
System.out.println(eggs.nextInt()); //System gets eggs(scanner) and ensures they are numbers. (1) Eggs gets the input.[in] (2) System prints it out.[out]
}
}
System.out.println(eggs.nextInt()); //System gets eggs(scanner) and ensures they are numbers. (1) Eggs gets the input.[in] (2) System prints it out.[out]
}
}
Words and numbers - Sentence code
class bananas
{
public static void main(String args[])
{
int bananas; //(int can be substituted with these)
bananas = 3;
System.out.print("I want ");
System.out.print(bananas);
System.out.println(" flowers");
}
}
{
public static void main(String args[])
{
int bananas; //(int can be substituted with these)
bananas = 3;
System.out.print("I want ");
System.out.print(bananas);
System.out.println(" flowers");
}
}
"Hello, world!" code
class bananas
{
public static void main(String args[])
{
System.out.println("Hello, world!");
}
}
{
public static void main(String args[])
{
System.out.println("Hello, world!");
}
}
Subscribe to:
Comments (Atom)