加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

我无法弄清楚我的简单Java作业

发布时间:2020-12-15 05:16:57 所属栏目:Java 来源:网络整理
导读:我有这个编程任务,可以在米和英尺之间,以及千克到磅之间进行转换.当我告诉程序我想要转换重量(通过在提示时输入“w”),它给了我以下错误: Error: Too many input characters error. 我在这方面工作了很长时间,但无法搞清楚.有人可以告诉我如何使重量转换像
我有这个编程任务,可以在米和英尺之间,以及千克到磅之间进行转换.当我告诉程序我想要转换重量(通过在提示时输入“w”),它给了我以下错误:

Error: Too many input characters error.

我在这方面工作了很长时间,但无法搞清楚.有人可以告诉我如何使重量转换像长度转换一样工作吗?

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;
        }
    }
}

解决方法

在将逻辑从一个地方粘贴到另一个地方时,通过不更改代码来犯了很多错误.通过减少重复可以大大改善你的代码,我会在’if”else’条件下更乐观地捕获正确的案例并将所有错误的案例留到最后……以下是工作版本通过修复拼写错误和逻辑顺序,稍微修改了您的代码.

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;

            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读