• Log in or sign up for an account here: https://www.sythe.org/login

First Java program made purely by myself.

slashshot007

Active Member
Joined
May 6, 2006
Sythe Gold
0
Messages
164
First Java program made purely by myself.
this is the first program i made by myself. i'm quite proud of it. constructive criticism is appreciated.

Code:
import java.util.Scanner;

public class tempurature {
    public static void main( String [] args ){
    	short loop = 2;      
    	while (loop == 2 ){
        	System.out.print( "If you would like to convert from Fahrenheit to Celsius, press 1 or,\n if you would like to convert Celsius to Fahrenheit, press 2: " );
        	Scanner input = new Scanner(System.in);  	
        	short stupid = input.nextShort();
        	if (stupid == 1){
         	   	System.out.print( "Enter Degrees Fahrenheit: " );
         	   	int f = input.nextInt();
    	       	double c = (5.0/9.0)*(f-32);
    	       	System.out.println( f + " Degrees Fahrenheit is equal to " + c + " Degrees Celsius.");
    	       	loop = 1;
    	    } else if (stupid == 2){
    	       	System.out.print( "Enter Degrees Celsius: ");
    	       	int c = input.nextInt();
    	       	double f = (9.0/5.0)*c+32;
   	         	System.out.print( c + " Degrees Celsius is equal to " + f + " Degrees Fahrenheit." );
   	         	loop = 1;
  	      	} else{
  	          	System.out.println( "Please Try Again. \n") ;
  	          	loop = 2;
  	    	}
		} 
	}
}
 
First Java program made purely by myself.
Uhmm that equation for conversion we learned in 6th grade lolll. But nice for a beginner.
 
First Java program made purely by myself.
updated it so if you don't enter 1 or two it loops the program.
 
First Java program made purely by myself.
Wait, your first program converts F to C. LELZ.
Umm, i still haven't memorized that equation <_<
 
First Java program made purely by myself.
Nice work. I suggest you improve on naming your variables a little bit. Variable names should tell anyone that reads the code exactly what a variable is used for. c and f are alright names, but in bigger programs, single character variable names are discouraged unless used inside for loops, etc.
 
First Java program made purely by myself.
I tidied it up for you:

Code:
import java.util.Scanner;

public class fcConverter {
	static double ConvertC(int Celcius) {
		return (double)(9.0/5.0)*Celcius+32;
	}
	
	static double ConvertF(int Fahrenheit) {
		return (double)(5.0/9.0)*(Fahrenheit-32);
	}
    
    public static void main(String[] args) {
    	boolean loop = true;
			
		System.out.println("Welcome to my Temperature Converter!\n");
		
		do {
			try {
				System.out.println("Press:\n\t'1' to convert Fahrenheit to Celcius\n\t'2' to convert Celcius to Fahrenheit\n\t'0' to quit");
				System.out.print("Enter your choice: ");
        		Scanner input = new Scanner(System.in);
        		short choice = input.nextShort();
        		switch(choice) {
        			case 0:
	        			System.out.println("\n\nThankyou for using my temperature converter!");
        				loop = false;
        				break;
        			case 1:
	        			System.out.print("Input Fahrenheit: ");
        				int ftemp = input.nextInt();
        				System.out.println(ftemp + " degrees Fahrenheit is " + ConvertF(ftemp) + " degrees Celcius.\n");
        				break;
        			case 2:
	        			System.out.print("Input Celcius: ");
        				int ctemp = input.nextInt();
        				System.out.println(ctemp + " degrees Celcius is  " + ConvertC(ctemp) + " degrees Fahrenheit.\n");
        				break;
        			default:
						System.out.println("Please enter either 1 or 2.\n");
        		}       			
        	} catch(java.util.InputMismatchException exc) {
				System.out.println("Error! Wrong input!\n");
			}
		} while(loop==true);
    }
}

output:
Welcome to my Temperature Converter!

Press:
'1' to convert Fahrenheit to Celcius
'2' to convert Celcius to Fahrenheit
'0' to quit
Enter your choice: 1
Input Fahrenheit: 90
90 degrees Fahrenheit is 32.22222222222222 degrees Celcius.

Press:
'1' to convert Fahrenheit to Celcius
'2' to convert Celcius to Fahrenheit
'0' to quit
Enter your choice: 2
Input Celcius: 30
30 degrees Celcius is 86.0 degrees Fahrenheit.

Press:
'1' to convert Fahrenheit to Celcius
'2' to convert Celcius to Fahrenheit
'0' to quit
Enter your choice: a
Error! Wrong input!

Press:
'1' to convert Fahrenheit to Celcius
'2' to convert Celcius to Fahrenheit
'0' to quit
Enter your choice: 0


Thankyou for using my temperature converter!

Remember: there are such primitive types as booleans, and when checking a true or false statement, its best to use the do-while to make it neater.

Tip: If you're learning how to manage input and output, a core thing to learn is exception handling. You see the 'try' and 'catch' statement in my code? Thats what generates the text "Error! Wrong input!" rather than crashing the program. Error handling is an art in itself, but its a core skill if you wish to program in Java.

Good for a first app. My first app was "hello world" from my book xD
 
Runescape Gamblingrunescape gambling
Back
Top