Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bin/App.class
Binary file not shown.
Binary file added src/App.class
Binary file not shown.
65 changes: 63 additions & 2 deletions src/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
import java.util.InputMismatchException;
import java.util.Scanner;

public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("\nTemperature Conversion Program ");
System.out.println("Enter your choice:");
System.out.println("1. Celsius to Fahrenheit");
System.out.println("2. Fahrenheit to Celsius");
System.out.println("0. Exit");
System.out.print("Enter your choice (1, 2, or 0): ");

int choice;
try {
choice = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number (1, 2, or 0).");
scanner.nextLine();
continue;
}


if (choice == 0) {
System.out.println("Exiting program");
break;
}
double temperature;
while (true) {
try {
System.out.print("Enter temperature to convert: ");
temperature = scanner.nextDouble();
break;
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
scanner.nextLine();
}
}


char conversionType = choice == 1 ? 'C' : 'F';


double convertedTemperature = convertTemperature(temperature, conversionType);
System.out.printf("The converted temperature is: %.2f\n", convertedTemperature);
}


scanner.close();
}

public static double convertTemperature(double temperature, char conversionType) {
if (conversionType == 'C') {
return (temperature * 9/5) + 32;
} else if (conversionType == 'F') {
return (temperature - 32) * 5/9;
} else {
System.out.println("Invalid conversion type. Please enter 'C' or 'F'.");
return Double.NaN;
}
}
}