conditions.java सम्पादन

// This program asks the user to select Fahrenheit or Celsius conversion
// and input a given temperature. Then the program converts the given
// temperature and displays the result.

import java.util.*;

class conditions
{
    private static Scanner input = new Scanner(System.in);
    
    public static void main(String[] args)
    {
        ifElse();
        switchCase();
    }

    private static void ifElse()
    {
        String choice;
        
        System.out.println("Enter F to convert to Fahrenheit or C to convert to Celsius:");
        choice = input.nextLine();
        if (choice.equals("C") || choice.equals("c"))
        {
            toCelsius();
        }
        else if (choice.equals("F") || choice.equals("f"))
        {
            toFahrenheit();
        }
        else
        {
            System.out.println("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
        }
    }

    private static void switchCase()
    {
        String choice;
        
        System.out.println("Enter F to convert to Fahrenheit or C to convert to Celsius:");
        choice = input.nextLine();
        switch(choice)
        {
            case "C":
            case "c":
                toCelsius();
                break;
            case "F":
            case "f":
                toFahrenheit();
                break;
            default:
                System.out.println("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
        }
    }

    private static void toCelsius()
    {
        double f;
        double c;
        
        System.out.println("Enter Fahrenheit temperature:");
        f = Double.parseDouble(input.nextLine());
        c = (f - 32) * 5 / 9;
        System.out.println(f + "° Fahrenheit is " + c + "° Celsius");
    }

    private static void toFahrenheit()
    {
        double c;
        double f;
        
        System.out.println("Enter Celsius temperature:");
        c = Double.parseDouble(input.nextLine());
        f = c * 9 / 5 + 32;
        System.out.println(c + "° Celsius is " + f + "° Fahrenheit");
    }
}

कोशिश करो सम्पादन

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के जावा कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें सम्पादन