MoMo
MoMo

Reputation: 47

How to increment a loop by 10 instead of 1?

This is my code as of right now: http://ideone.com/PfXNQc

import java.util.Scanner;

public class Temperature
{

    public static double convertFtoC(double F)
    {
        return (F-32)*5/9;
    }

    public static void main(String[] args)
    {
        double F; //Stores Farenheit
        double C; //Stores Celsius

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a temperature in Fahrenheit: "); //Enter in Farenheit
        F = keyboard.nextDouble(); //Stores Farenheit

        System.out.println("The temperature in Celsius is: " + convertFtoC(F)); //Displays Celsius (call convertFtoC(F) where celcuis conversion needed)

        F = 0;

        System.out.println("Fahrenheit\t Celsius"); 
        while (F <= 100)                    
        {
            // Display the F and C in increments of 10.
            System.out.println(F + "\t\t" + convertFtoC(F));
            // Increment for the next day.
            F++;
        }       
    }
}

I would like the loop for conversions to go up in increments of 10 rather than by 1.

Right now it is outputting this:

incorrect output

But I would like it to be

0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100

Upvotes: 3

Views: 11582

Answers (5)

TryinHard
TryinHard

Reputation: 4118

For enhanced readability:

  1. Don't use Capitalized identifiers for the variables as they are meant for the constants in Java.
  2. Use meaningful identifier names.

Here is the code:

import java.util.Scanner;

public class Temperature
{

    public static double convertFarenheitToCelsius(double farenheit)
    {
        return (farenheit-32)*5/9;
    }

    public static void main(String[] args)
    {
        double farenheit; //Stores Farenheit
        double celsius; //Stores Celsius

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a temperature in Fahrenheit: "); //Enter in Farenheit
        if (keyboard.hasNextDouble()){
            F = keyboard.nextDouble(); //Stores Farenheit

            System.out.println("The temperature in Celsius is: " + convertFarenheitToCelsius(farenheit)); //Displays Celsius (call convertFarenheitToCelsius(farenheit) where celcius conversion needed)
            farenheit = 0;
            System.out.println("Fahrenheit\t Celsius"); 
            while (farenheit <= 100){
                System.out.println(farenheit + "\t\t" + convertFarenheitToCelsius(farenheit));
                farenheit += 10;
            }   
        } else {
            System.err.format("%s is not a double!%nPlease enter a double value.", keyboard.next());
            System.exit(1);
        }   
    }
}

Upvotes: 1

user177800
user177800

Reputation:

Modern Idiomatic Professional Approach

Q32835243.java

package com.stackoverflow;

import java.util.Scanner;

public class Q32835243
{
    private static double farhenheitToCelsius(final double fahrenheit) {return (fahrenheit - 32) * 5 / 9;}

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

        System.out.print("Enter a temperature in Fahrenheit: ");
        if (keyboard.hasNextDouble())
        {
            final double fahrenheit = keyboard.nextDouble();
            final double celsius = farhenheitToCelsius(fahrenheit);
            System.out.format("The temperature in Celsius is: %8.4f%n", celsius);
            System.out.format("Fahrenheit\t Celsius%n");

            for (int i=0; i < 100; i+=10)
            {
                System.out.format("%8.4f\t%8.4f%n", fahrenheit + i, farhenheitToCelsius(fahrenheit + i));
            }
        }
        else
        {
            System.err.format("%s is not a double!%nPlease enter a double value.", keyboard.next());
            System.exit(1);
        }
    }
}

Link to this solution as a fork of what you did.

I put this working code on ideone.com so you could easily see the differences and how to make this trival code bullet proof as possible.

Things to take note of in this solution:

  1. Always make as many things final as possible. It eliminates two of the most common classes of program errors NullPointerException and side effects.

  2. Always limit the scope of variable references and visibility of methods as much as possible. You want to define variables when you use them and make as many things private as you can.

  3. Always name your variables in the context that you are using them so that you do not need to put comments explaining what they are.

  4. Comments should only be in clean code when you are deviating from the expected. All your comments were useless tautology and violate the DRY principle as well. If things change now you have to update the code and the comments. More to maintain, easier to just name things that are self explanatory.

  5. Using single letter variable names in short functions is fine. In the context of the block f and c would be acceptable names for fahrenheit and celsius if they were object references. But in the case of homework explicit is always better for communicating exactly what you are thinking.

  6. Always follow the style guide when using lowerCamelCase and UpperCamelCase. Know when to use each in the correct context.

  7. Always check your inputs and handle invalid data. Never try and fix it, if it is wrong stop the program, explain to the user where they went wrong and end the program. Defensive programming is terrible.

  8. Always use the features of the different objects to your advantage. Use the String.format() and PrintStream.format() to eliminate formatting issues and making things more maintainable.

  9. Learn to use the Scanner class correctly. the .hasNextXxx() methods are there for a reason, use them! The scanner class has a terrible API and is the source of more frustration to use programmers than just about anything else.

Upvotes: 3

RojoSam
RojoSam

Reputation: 1496

I think in your case use FOR is a better choice.

public static void main(String[] args) {
    double fahrenheit = 50;
    double increment = 10;
    for(;fahrenheit <= 100; fahrenheit += increment){
        System.out.println(fahrenheit);
    }
}

Upvotes: 0

nomadus
nomadus

Reputation: 909

what if you write as follows.

while (F <= 90) //Last number before 100 is 90
{
        // Display the F and C in increments of 10.
        System.out.println(F + "\t\t" + convertFtoC(F));
        // Increment for the next day.
        F=F+10;
}

Additionally,why you use capitals for variables, per conventions you should start variable names with lower case letters, objects names are started with capitals.

Upvotes: 3

Nikita Shrivastava
Nikita Shrivastava

Reputation: 3018

Set F+=10 instead of F++ in the loop.

Upvotes: 0

Related Questions