mvanderk10
mvanderk10

Reputation: 51

using methods for the first time for temperature converter

I am using methods for the first time, my program works mostly, except it wont loop the conversion statement with the question it corresponds to...The user input corresponds with the conversion statement, so 3 times will prompt

Conversion #1

...

Conversion #2

...

Conversion #3

...

also the conversion between farenheight to celsius works but not from celsius to farenheight, any insight would be helpful below is my code,

import java.util.Scanner;
import java.text.DecimalFormat;

    public class TempConverter {

        public static void main(String[] args){


            DecimalFormat df = new DecimalFormat("#,###.0");
            System.out.println("Temperature Converter");
            System.out.println("---------------------");
            System.out.println();
             Scanner input = new Scanner(System.in);

              System.out.print("How many conversions would you like to make: ");
              int conversions=input.nextInt();

              for(int i = 1; i < conversions; i++){
              System.out.println("Conversion # " + i++);
              System.out.println();
              System.out.println ("To convert from celsius to fahrenheit type 1 ");
              System.out.print ("To convert from fahrenheit to celsius type 2: ");
              int choice=input.nextInt();

                switch(choice){

                case 1:
                     System.out.println();
                     System.out.print ("Enter a celsius temperature: ");
                     double cels=input.nextDouble();
                     double result=celsiusToFahrenheit(choice,cels);
                     System.out.println();
                     System.out.println ("The conversion of "+cels+" from celcius to fahrenheit is "+df.format(result) );
                     break;
                case 2:
                     System.out.println();
                     System.out.print("Enter a farenheight temperature: ");
                     double fahr=input.nextDouble();
                     double result1=fahrenheitToCelsius(choice,fahr);
                     System.out.println ("The conversion of "+fahr+" from fahrenheit to celcius is "+df.format(result1) );

                }
              }
           }



            public static double celsiusToFahrenheit(int choice, double cels)
            {

             double converted=0.0;
              if (choice == 1)
              converted=9.0/5.0*cels+32;

              return converted;
              }

            public static double fahrenheitToCelsius(int choice, double fahr)
            {
                double converted2=0.0;
                if (choice ==2)
                   converted2=5.0/9.0*(fahr-32);

                   return converted2;
            }

            }

Upvotes: 0

Views: 378

Answers (2)

Dipali Vasani
Dipali Vasani

Reputation: 2536

loop code is wrong and switch case2 is not having break. find corrected code here :

import java.text.DecimalFormat;
import java.util.Scanner;

public class TempConverter {

    public static void main(String[] args) {

        DecimalFormat df = new DecimalFormat("#,###.0");
        System.out.println("Temperature Converter");
        System.out.println("---------------------");
        System.out.println();
        Scanner input = new Scanner(System.in);

        System.out.print("How many conversions would you like to make: ");
        int conversions = input.nextInt();

        for (int i = 1; i <= conversions; i++) {
            System.out.println("Conversion # " + i);
            System.out.println();
            System.out.println("To convert from celsius to fahrenheit type 1 ");
            System.out.print("To convert from fahrenheit to celsius type 2: ");
            int choice = input.nextInt();
            switch (choice) {
            case 1:
                System.out.println();
                System.out.print("Enter a celsius temperature: ");
                double cels = input.nextDouble();
                double result = celsiusToFahrenheit(choice, cels);
                System.out.println();
                System.out.println("The conversion of " + cels + " from celcius to fahrenheit is " + df.format(result));
                break;
            case 2:
                System.out.println();
                System.out.print("Enter a farenheight temperature: ");
                double fahr = input.nextDouble();
                double result1 = fahrenheitToCelsius(choice, fahr);
                System.out
                        .println("The conversion of " + fahr + " from fahrenheit to celcius is " + df.format(result1));
                break;
            }
        }
    }

    public static double celsiusToFahrenheit(int choice, double cels) {

        double converted = 0.0;
        if (choice == 1)
            converted = 9.0 / 5.0 * cels + 32;

        return converted;
    }

    public static double fahrenheitToCelsius(int choice, double fahr) {
        double converted2 = 0.0;
        if (choice == 2)
            converted2 = 5.0 / 9.0 * (fahr - 32);

        return converted2;
    }

}

Upvotes: 0

ajb
ajb

Reputation: 31699

You have two mistakes:

(1) This line is wrong:

for(int i = 1; i < conversions; i++)

It means to loop as long as i < conversions. If conversions is 3, that means it will loop with i==1 and i==2, but it won't loop for i==3 because 3<3 is false. Use <=.

(2) The i++ in the above for statement will increment i every time it loops back. This is what you want. However, you're defeating it by putting another i++ in the code, which will increment it an extra time.

Upvotes: 1

Related Questions