Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
允许Java识别不等于所需值的输入_Java - Fatal编程技术网

允许Java识别不等于所需值的输入

允许Java识别不等于所需值的输入,java,Java,我对Java和这个网站都是新手,所以请原谅我可能犯的任何错误,我已经不知所措了 我正在尝试制作一个程序,通过不同的媒介计算声音的速度。到目前为止,程序将要求用户输入距离并允许输入,媒体也是如此。我创建了几个案例来计算正确工作的给定介质的答案 问题是当我试图创建一个循环,识别媒体输入是否不是可用的4个选项之一时。我成功地创建了一个循环,可以识别距离输入是否不是数字值,并尝试使用类似的原理进行中等输入,但要么一直卡在无限循环中,要么在输入正确选项时收到我为错误输入创建的消息。 我已经尝试了我所学过的

我对Java和这个网站都是新手,所以请原谅我可能犯的任何错误,我已经不知所措了

我正在尝试制作一个程序,通过不同的媒介计算声音的速度。到目前为止,程序将要求用户输入距离并允许输入,媒体也是如此。我创建了几个案例来计算正确工作的给定介质的答案

问题是当我试图创建一个循环,识别媒体输入是否不是可用的4个选项之一时。我成功地创建了一个循环,可以识别距离输入是否不是数字值,并尝试使用类似的原理进行中等输入,但要么一直卡在无限循环中,要么在输入正确选项时收到我为错误输入创建的消息。 我已经尝试了我所学过的基本循环:for、do-while等,但是我被卡住了。非常感谢您的任何建议

public static void main(String[] args) {
    //prompt the user about the purpose of this program

    System.out.println(" The purpose of this program is to calculate the speed of sound through several mediums.\n The program user will input a distance in feet followed by a mediums and the program will output the speed in feet per second and miles per hour\n");
    //declare variables

    Scanner keyboard = new Scanner(System.in);

    final double Air = 1126.1;

    final double Water = 4603.2;

    final double Steel = 20013.3;

    final double Earth = 22967.4;

    double OneFootPerSecond = .68181818182;

    double Distance;

    double AirSpeed;

    double WaterSpeed;

    double SteelSpeed;

    double EarthSpeed;

    System.out.print(" What is the distance in feet:" );
    //ask the user to input variables


        while (!keyboard.hasNextDouble()){
        System.out.println("Please enter a valid numeric value, try again: ");
        keyboard.next();
        }
        Distance =keyboard.nextDouble();
        {
        System.out.print("Input the media: Air, Water, Steel, or Earth: ");
        String Input = keyboard.next();   
        Input.toLowerCase();

        switch (Input)

         {

            case "air":
            AirSpeed = Distance/Air;
            System.out.print("\n \nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through AIR" +"\n");
            System.out.printf("%.6f", AirSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Air);
            System.out.print(" miles per hour."); 
            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();

            break;


     case "water":
            WaterSpeed = Distance/Water;
            System.out.print("\nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through WATER" +"\n");
            System.out.printf("%.6f",WaterSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Water);
            System.out.print(" miles per hour."); 
    break;

     case "steel":
            SteelSpeed = Distance/Steel;
            System.out.print("\nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through STEEL" +"\n");
            System.out.printf("%.6f",SteelSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Steel);
            System.out.print(" miles per hour."); 
    break;     

          case "earth":
            EarthSpeed = Distance/Water;
            System.out.print("\nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through EARTH" +"\n");
            System.out.printf("%.6f",EarthSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Earth);
            System.out.print(" miles per hour."); 
    break;

首先,正如注释中所建议的,更改
Input.toLowerCase()类似于:

Input = Input.toLowerCase();
或将开关(输入)
更改为:

switch(Input.toLowerCase())
现在来看看有趣的部分

添加
默认值:
块,而不是新单词的大小写。如果没有其他匹配项,则将运行此操作

switch (Input)

    {

        case "air":
            AirSpeed = Distance/Air;
            System.out.print("\n \nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through AIR" +"\n");
            System.out.printf("%.6f", AirSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Air);
            System.out.print(" miles per hour."); 
            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();
        break;


        // Some more cases...


        default:
            // Something something that will happen as a "last resort".

        break;

  } // End switch
没问题

我看到以下问题:

Input.toLowerCase()

这不是工作用途

Input=Input.toLowerCase()


因为函数只返回一个新字符串

Input.toLowerCase()不做任何事情,因为它返回字符串的小写字母。请尝试
switch(Input.toLowerCase()){
,或者更好地创建一个您打开的枚举。这意味着您的switch语句永远不会被(潜在的)未来程序员弄乱。在这种情况下,这是一个糟糕的示例,因为您正在切换用户输入(字符串),但不要养成在有可用枚举时切换字符串的习惯。:)如我下面所说,我根据建议更改了输入,这很有帮助,我还添加了默认值,这也很有帮助。但是,如何让用户无限次地输入介质,直到选择正确的介质?编辑:并对其中一种情况使用输入如果匹配?您需要阅读Java样式指南:。变量名称不应以大写字母开头。请注意,这只是一个语法习惯!我根据建议更改了输入,这很有帮助,我还添加了默认值,这也很有帮助。但是,如何让用户无限次地输入介质,直到选择正确的一个?编辑:如果匹配,还可以使用其中一个案例的输入?@user2824855:您可以使用while循环,只要
布尔quitLoop
不是true,就可以循环。如果用户输入“quit”而不是“air”或任何其他可能性,则可以将其设置为true。然后,您可以设置与“quit”匹配的案例,并将
quitLoop
设置为
true
。当开关断开且循环重新启动时,
quitLoop
将为
true
,这意味着
!quitLoop
将为
false
,循环将结束。