如何用数组减去/除

时间:2013-11-19 22:50:36

标签: java

//Declare and intialize variables - programmer to provide initial values
    Scanner in = new Scanner(System.in);
    String city = "Daytona Beach";
    String state = "Florida";
    double a =0;

    String month [] ={"Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    double temperature [] = {58.4, 60.0, 64.7, 68.9,74.8, 79.7, 81.7, 81.5,79.9,74.0,67.0,60.8};        
    double precipitation [] ={3.1, 2.7,3.8,2.5,3.3,5.7,5.2,6.1,6.6,4.5,3.0,2.7};    
    String tempLabel = "F";   //initialize to F
    String precipLabel = "inch"; //initialize to inch

    double c [] = {32.0};
    double q [] = {0.5555};

    //INPUT - ask user for temp and preciptation scale choice
    System.out.print("Choose the temperature scale (F = Fahrenheit, C = Celsius): ");
    String tempChoice = in.next();
    System.out.print("Choose the precipitation scale (i = inches, c = centimeteres): ");
    String precipChoice = in.next();


    //PROCESSING - convert from F to C and in to cm based on user's choices

    // remember 5/9 = 0, 5.0/9 = .5555

    if(tempChoice.equalsIgnoreCase("C"))
    {
        tempLabel="(C)";


        for( int index = 0; index < temperature.length; index++)
        {

        }

    }

    //Convert in values to cm; replace the current values in precipitation
    if(precipChoice.equalsIgnoreCase("c"))
    {
        precipLabel="(cm)";


    }

    //OUTPUT - print table using printf to format and align data

    System.out.println();
    System.out.println("Climate Data");
    System.out.println("Location: " + city +", " + state);
    System.out.printf("%5s %18s %s %18s %s","Month","Temperature",tempLabel,"Precipitation",precipLabel);
    System.out.printf("***************************************************");
    System.out.printf("%s\n",month);
    System.out.println();
    System.out.printf("***************************************************");
    System.out.println();

不要担心转换降水。如何转换多个数组值(华氏温度)?我试过(例子):a [i] = b [i] -c [i]但总是得到错误来关闭温度数组。错误是“']'是预期的。

**我也收到编译器警告“上次编译警告:

  • 非varargs使用不精确的参数类型调用varargs作为最后一个 周长;

  • 强制转换为java.lang.Object以进行varargs调用

  • 转换为java.lang.Object []进行非varargs调用并禁止 警告

3 个答案:

答案 0 :(得分:2)

如果您在示例中引用的a是您声明的double a,那么因为a不是数组。

c和q也应该是普通的double而不是数组

答案 1 :(得分:1)

我会尽量简单。

for( int index = 0; index < temperature.length; index++) {
    double celsius = (temperature[i] - 32)*5/9;

答案 2 :(得分:0)

试试这个:

if(tempChoice.equalsIgnoreCase("C"))
{
    tempLabel="(C)";


    for( int index = 0; index < temperature.length; index++)
    {
        temperatue = (temperature[index] - 32)*5/9;
        temperature[index] = temperatue;
    }

}