Login Register






[Java] My First Program filter_list
Author
Message
[Java] My First Program #1
Hey HackCommunity!
As some of you may know, I'm learning java. I just wanted to post my progress and see what you guys think. I have been getting help from a couple people. Especially one who has helped me come up with these programs to make (you know who you are) Smile Anyways, just wanted to post these SUPER simple programs to use as an example for those in my skill level area, and for others to critique. Anyways, here they are.

This first one is a calculator:
Code:
import java.util.Scanner; public class calculator { public static void main(String args[]) { Scanner lightx = new Scanner(System.in); double fnum, snum, answer = 0, operation; System.out.println("Enter your first number: "); fnum = lightx.nextDouble(); System.out.println("Enter your second number: "); snum = lightx.nextDouble(); System.out.println("What operation would you like to use? (+ = 1, - = 2, * = 3, / = 4)"); operation = lightx.nextDouble(); if (operation == 1){ answer = fnum + snum; } if (operation == 2){ answer = fnum - snum; } if (operation == 3){ answer = fnum * snum; } if (operation == 4){ answer = fnum / snum; } System.out.println(answer); } }

This next one is just a simple program that counts from 0 to the number that you tell it to:
Code:
import java.util.Scanner; public class LightX { public static void main(String args[]){ Scanner number = new Scanner(System.in); int lightx = 0; System.out.println("What would you like the number to end on?"); int num = number.nextInt(); while (lightx <= num){ System.out.println(lightx); lightx++; } } }

I also have one for just evens, which is just the same code but with one more variable++ (lightx++):
Code:
import java.util.Scanner; public class LightX { public static void main(String args[]){ Scanner number = new Scanner(System.in); int lightx = 0; System.out.println("What would you like the number to end on?"); int num = number.nextInt(); while (lightx <= num){ System.out.println(lightx); lightx++; lightx++; } } }

Reply





Messages In This Thread
[Java] My First Program - by LightX - 02-21-2013, 10:29 PM
[Java] My First Program - by LightX - 02-21-2013, 10:29 PM