Search This Blog

Wednesday, December 14

!ANSWER! Assignment 1


Below you will find my answer to last week's Java assignment.

Hope this helps!


/**
import java.awt.Choice;
import java.util.*;

public class BajrovicAssignment1 {
	public static void main(String[] args){
		while(true){
		Scanner in = new Scanner(System.in);
		String[] menu = new String[4];
		menu[0] = "Please choose an option for temperture conversion below:";
		menu[1] = "1: Fahrenheit to Celsius";
		menu[2] = "2: Celsius to Fahrenheit";
		menu[3] = "x: Exit";
	for (String m : menu)	//prints the entire array with only 1 system.out 
		System.out.println(m);
	String choice = in.nextLine();
	
	if (choice.equals("1")){
		System.out.println("What Fahrenheit temperature would you like to convert?");
		float Fahrenheit = in.nextFloat();
		float Celsius = (Fahrenheit-32)*5/9;
		System.out.println(Celsius);
	}
	else if (choice.equals("2")){
		System.out.println("What Celsius temperature would you like to convert?");
		float Celsius = in.nextFloat();
		float Fahrenheit = Celsius*9/5+32;
		System.out.println(Fahrenheit);
	}
	else if (choice.equals("x")){
		System.exit(0);
	}
	else{
				System.out.println("Invalid Selection.");
			}
		}
			
		}
	}
How did you solve it? What could I have done differently?

No comments:

Post a Comment