vendredi 21 août 2020

If statement loop

So I made a program to do different math problems depending on what the user wants, and I don't want the program to end after the user is done. Right now, the loop I have just repeats the same option over and over. The desired effect is to be able to keep choosing which problem you want to do. I am fairly new to coding so I apologize if this is a simple question.

public static void main(String[] args)
{
    System.out.println("What problem do you want to do?");
    System.out.println("1:Celcius to Farenheit");
    System.out.println("2:Slope Formula");
    System.out.println("3:Averages");
    System.out.println("4:Perimeter");
    System.out.println("5:Exit\n\n");
    
    Scanner keyboard = new Scanner(System.in);
    
    int response = keyboard.nextInt();

    
    do
    {
    if (response == 1)
    {
        System.out.println("Degrees Celcius:");
        int degrees = keyboard.nextInt();
        System.out.println(degrees + " degrees Celcius is " + celsToFarenheit(degrees) + " degrees Farenheit.");
    }
    else if (response == 2)
    {
        System.out.println("x1:");
        int x1 = keyboard.nextInt();
        System.out.println("y1:");
        int y1 = keyboard.nextInt();
        System.out.println("x2:");
        int x2 = keyboard.nextInt();
        System.out.println("y2:");
        int y2 = keyboard.nextInt();
        System.out.println("The slope is " + slopeFormula(x1,y1,x2,y2));
    }
    else if (response == 3)
    {
        System.out.println("First number:");
        int num1 = keyboard.nextInt();
        System.out.println("Second number:");
        int num2 = keyboard.nextInt();
        System.out.println("The average is " + average(num1,num2));
    }
    else if (response == 4)
    {
        System.out.println("Length:");
        int length = keyboard.nextInt();
        System.out.println("Width:");
        int width = keyboard.nextInt();
        System.out.println("The perimeter is " + perimeter(length,width));
    }
}
while (response != 5);
    

Aucun commentaire:

Enregistrer un commentaire