I can’t figure out my simple java homework

I have this programming task, which can convert between meters and feet, and between kilograms and pounds When I told the program that I wanted to convert the weight (by entering "W" at the prompt), it gave me the following error:

I've been working on this for a long time, but I can't figure it out Can someone tell me how to make weight conversion work like length conversion?

import java.util.Scanner;

/**
 * This class..
 */
public class UnitConversion3b
{
    public static void main(String[] args)   
    {
        Scanner keyboard = new Scanner(system.in);

        String maxInputWarning = "\nError: Too many input characters."
        + "\nProgram is Now terminating.";
        String lengthOrWeight;
        final double LENGTH_CONVERSION_FACTOR = 3.2808399;
        final double WEIGHT_CONVERSION_FACTOR = 2.20462;
        String whichWeightConversion = "empty",whichLengthConversion = "empty";
        double feet = 0,meters = 0,pounds =0,kilograms = 0;
        double metersConvertedToFeet,feetConvertedToMeters;
        double poundsConvertedToKilograms,kilogramsConvertedToPounds;

        System.out.println("");
        System.out.print("What kind of value would you like to convert?");
        System.out.print("\nEnter L for length,or W for weight: ");
        lengthOrWeight = keyboard.nextLine();
        if (lengthOrWeight.length() > 1 ) {
            System.out.println(maxInputWarning);
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if ((!(lengthOrWeight.equalsIgnoreCase("l"))
            && (!(lengthOrWeight.equalsIgnoreCase("w"))))){
            System.out.println("\nError: Unrecognized conversion type."
            + "\nProgram is Now terminating.");
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (lengthOrWeight.equalsIgnoreCase("l")){
            System.out.println("\nConverting feet or meters?");
            System.out.print("Enter F to convert feet,or M for meters: "); 
            whichLengthConversion = keyboard.nextLine();
        }

        if (whichLengthConversion.length() > 1 ) {
            System.out.println(maxInputWarning);
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if ((!(whichLengthConversion.equalsIgnoreCase("f"))
            && (!(whichLengthConversion.equalsIgnoreCase("m"))))){
            System.out.println("\nError: Unrecognized unit of "
            + "measurement.\nProgram is Now terminating."     );
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (whichLengthConversion.equalsIgnoreCase("f")){
            System.out.print ("Enter the number of feet to"
            + " convert to meters: ");
            feet = keyboard.nextDouble();
            feetConvertedToMeters = feet / LENGTH_CONVERSION_FACTOR;
            System.out.println("The number of meters in " + feet +
            " feet is " + feetConvertedToMeters + ".");
            keyboard.nextLine();
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (whichLengthConversion.equalsIgnoreCase("m")){
            System.out.print ("Enter the number of meters to"
            + " convert to feet: ");
            meters = keyboard.nextDouble();
            metersConvertedToFeet = meters * LENGTH_CONVERSION_FACTOR;
            System.out.println("The number of feet in " + meters +
            " meters is " + metersConvertedToFeet + ".");
            keyboard.nextLine();
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        }

        if (lengthOrWeight.equalsIgnoreCase("w")){
            System.out.println("Converting pounds or kilograms?");
            System.out.print("Enter P to convert pounds,or K for kilograms: ");
            whichWeightConversion = keyboard.nextLine();
        }

        if (whichWeightConversion.length() > 1 ) { 
            System.out.println(maxInputWarning);
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if ((!(whichWeightConversion.equalsIgnoreCase("p"))
            && (!(whichWeightConversion.equalsIgnoreCase("k"))))){
            System.out.println("\nError: Unrecognized unit of "
            + "measurement.\nProgram is Now terminating."     );
            System.out.print("Press Enter to continue ... ");
            return;
        } else if (whichWeightConversion.equalsIgnoreCase("p")){
            System.out.println("Enter the number of pounds to"
            + " convert to kilograms:");
            pounds = keyboard.nextDouble();
            poundsConvertedToKilograms = pounds / WEIGHT_CONVERSION_FACTOR;
            System.out.println("The number of pounds in " + kilograms +
            " kilograms is " + poundsConvertedToKilograms + ".");
            keyboard.nextLine();
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (whichLengthConversion.equalsIgnoreCase("k")){
            System.out.print ("Enter the number of kilograms to"
            + " convert to pounds: ");
            kilograms = keyboard.nextDouble();
            kilogramsConvertedToPounds = kilograms * WEIGHT_CONVERSION_FACTOR;
            System.out.println("The number of pounds in " + pounds +
            "pounds is " + kilogramsConvertedToPounds + ".");
            keyboard.nextLine();
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;

        } else{ 
            return;
        }
    }
}

Solution

When pasting logic from one place to another, you make a lot of mistakes by not changing the code By reducing repetition, you can greatly improve your code. I will be more optimistic to capture the correct cases under the condition of 'if' else 'and leave all wrong cases to the end... The following is the working version, which slightly modifies your code by fixing spelling errors and logical order

import java.util.Scanner;

public class UnitConversion3b {
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(system.in);

        String maxInputWarning = "\nError: Too many input characters."
                + "\nProgram is Now terminating.";
        String lengthOrWeight;
        final double LENGTH_CONVERSION_FACTOR = 3.2808399;
        final double WEIGHT_CONVERSION_FACTOR = 2.20462;
        String whichWeightConversion = "empty",pounds = 0,or W for weight: ");

        lengthOrWeight = keyboard.nextLine();
        if (lengthOrWeight.length() > 1) {
            System.out.println(maxInputWarning);
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if ((!(lengthOrWeight.equalsIgnoreCase("l")) && (!(lengthOrWeight
                .equalsIgnoreCase("w"))))) {
            System.out.println("\nError: Unrecognized conversion type."
                    + "\nProgram is Now terminating.");
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (lengthOrWeight.equalsIgnoreCase("l")) {
            System.out.println("\nConverting feet or meters?");
            System.out.print("Enter F to convert feet,or M for meters: ");
            whichLengthConversion = keyboard.nextLine();

            if (whichLengthConversion.length() > 1) {
                System.out.println(maxInputWarning);
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if ((!(whichLengthConversion.equalsIgnoreCase("f")) && (!(whichLengthConversion
                    .equalsIgnoreCase("m"))))) {
                System.out.println("\nError: Unrecognized unit of "
                        + "measurement.\nProgram is Now terminating.");
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if (whichLengthConversion.equalsIgnoreCase("f")) {
                System.out.print("Enter the number of feet to"
                        + " convert to meters: ");
                feet = keyboard.nextDouble();
                feetConvertedToMeters = feet / LENGTH_CONVERSION_FACTOR;
                System.out.println(feet + " Feet in Meters is "
                        + feetConvertedToMeters + ".");
                keyboard.nextLine();
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if (whichLengthConversion.equalsIgnoreCase("m")) {
                System.out.print("Enter the number of meters to"
                        + " convert to feet: ");
                meters = keyboard.nextDouble();
                metersConvertedToFeet = meters * LENGTH_CONVERSION_FACTOR;
                System.out.println(meters + " Meters in Feet is "
                        + metersConvertedToFeet + ".");
                keyboard.nextLine();
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            }
        }

        else {
            System.out.println("Converting pounds or kilograms?");
            System.out.print("Enter P to convert pounds,or K for kilograms: ");
            whichWeightConversion = keyboard.nextLine();

            if (whichWeightConversion.length() > 1) {
                System.out.println(maxInputWarning);
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if ((!(whichWeightConversion.equalsIgnoreCase("p")) && (!(whichWeightConversion
                    .equalsIgnoreCase("k"))))) {
                System.out.println("\nError: Unrecognized unit of "
                        + "measurement.\nProgram is Now terminating.");
                System.out.print("Press Enter to continue ... ");
                return;
            } else if (whichWeightConversion.equalsIgnoreCase("p")) {
                System.out.println("Enter the number of pounds to"
                        + " convert to kilograms:");
                pounds = keyboard.nextDouble();
                poundsConvertedToKilograms = pounds / WEIGHT_CONVERSION_FACTOR;
                System.out.println(pounds + " Pounds in Kilograms is "
                        + poundsConvertedToKilograms + ".");
                keyboard.nextLine();
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if (whichWeightConversion.equalsIgnoreCase("k")) {
                System.out.print("Enter the number of kilograms to"
                        + " convert to pounds: ");
                kilograms = keyboard.nextDouble();
                kilogramsConvertedToPounds = kilograms
                        * WEIGHT_CONVERSION_FACTOR;
                System.out.println(kilograms + " Kilograms in Pounds is "
                        + kilogramsConvertedToPounds + ".");
                keyboard.nextLine();
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;

            }
        }
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>