这段代码不断获取其他信息,如果出现错误

时间:2018-08-06 07:21:06

标签: java

如果没有其他错误,此代码将不断获取。我在这里看不到问题。请帮忙。

import java.util.Scanner;

public class TempOut {
    public static void main(String[] args) {
        //variable
        double t;
        double v;
        Scanner input = new Scanner(System.in);

        //inputs
        System.out.print("Give temperature(C): ");
        t = input.nextDouble();
        System.out.print("Give wind velocity(km/h): ");
        v = input.nextDouble();

        //formula
        if (((t>=-50) & (t=<5)) | 3>=v) 
        {
            double formula = 13.12 + (2*(0.6251*t)) - (11.37*v) + ((0.3965*t)*v);
            System.out.print("The wind chill temperature is : "+formula);
        }
        else {
            if (((t<-50) & (t>5)) | v=<3) 
            {
            System.out.print("Formula not suitable");
            }
            else {
                System.out.print("Invalid input");
            }
        }
    }
}

它说在每个“其他”上都有一个错误。

3 个答案:

答案 0 :(得分:0)

 if (((t>=-50) & (t=<5)) | 3>=v) 

都错了:

最后,您的if语句应为:

if (((t >= -50) && (t <= 5)) || 3 >= v) 

答案 1 :(得分:0)

您在代码中if-conditions都有很多错误。

  1. 您使用的是&而不是&&|而不是||
  2. 您使用的是=<而不是<=

修改后的代码:-

import java.util.Scanner;

public class Main {// TempOut {
    public static void main(String[] args) {
        // variable
        double t;
        double v;
        Scanner input = new Scanner(System.in);

        // inputs
        System.out.print("Give temperature(C): ");
        t = input.nextDouble();
        System.out.print("Give wind velocity(km/h): ");
        v = input.nextDouble();

        // formula
        if ((t >= -50 && t <= 5) || (3 >= v)) // not if (((t>=-50) & (t=<5)) | 3>=v)
        {
            double formula = 13.12 + (2 * (0.6251 * t)) - (11.37 * v) + ((0.3965 * t) * v);
            System.out.print("The wind chill temperature is : " + formula);
        } else {
            if (((t < -50) && (t > 5)) || v <= 3) // not if (((t<-50) & (t>5)) | v=<3)
            {
                System.out.print("Formula not suitable");
            } else {
                System.out.print("Invalid input");
            }
        }
    }
}

请确保您没有犯语法错误。

输出:-

Give temperature(C): 2
Give wind velocity(km/h): 5
The wind chill temperature is : -37.26459999999999

答案 2 :(得分:0)

正确的代码:

if (((t>=-50) & (t>=5)) | 3>=v) 
        {
            double formula = 13.12 + (2*(0.6251*t)) - (11.37*v) + ((0.3965*t)*v);
            System.out.print("The wind chill temperature is : "+formula);
        }
        else {
            if (((t<-50) & (t>5)) | v>=3) 
            {
            System.out.print("Formula not suitable");
            }
            else {
                System.out.print("Invalid input");
            }
        }

您正在使用
if(((t> =-50)&(t = <5))| 3> = v )和if(((t <-50)&(t> 5))| < strong> v = <3 )。

在正确的代码和您的代码中仔细查看此条件(粗体字符)。

相关问题