有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

语法java基本数组问题

List[K] = new Station(Location, Temp, Name);

在一种方法中,我输入位置和名称的值。在后面的方法中,我想循环遍历每个位置并分配一个唯一的临时值

       public void Post() 
{
     double Temp = 0;
     int K;
     for(K = 0; K < Count ; K++)
     System.out.print(" " + List[K].GetLocation() + ": ");
     System.out.println("Enter Temperature");
     Temp = Input.nextDouble();

}

这只是转储所有位置,然后提示输入温度并接受它。但温度甚至没有指定给数组中的最后一个值


共 (3) 个答案

  1. # 1 楼答案

    为什么Temp在第一行初始化?为什么最后一行有任务?它没有效果

    为什么要在循环外声明K? Codeconventions要求属性、参数、变量和方法使用小写字母

      list[k] = new Station (location, temp, name);
    // 
    
    public void post () 
    {
         for (int k = 0; k < count; k++)
             System.out.print (" " + list[k].getLocation () + ": ");
         System.out.println ("Enter Temperature");
         double Temp = Input.nextDouble ();
    }
    

    In one method I enter in the values for Location and Name. In a later method I would like to loop through each Location and assign a unique Temp.

    嗯,你在车站里转,不是在地点,是吗

    But the temperature is not even assigned to the last value in the array.

    哪个阵列?数组名为List?你从来没有给某件事分配临时工,是吗

    车站里有二传手吗?它是一个可写属性吗

    list[count-1].setTemp (temp); // or 
    list[count-1].temp = temp; 
    

    也许会有帮助

  2. # 2 楼答案

    这就是你想做的吗?打印出列表中的每个位置,并向用户请求温度

       public void Post() 
    {
     double Temp = 0;
     int K;
     for(K = 0; K < Count ; K++)
     {  
      System.out.print(" " + List[K].GetLocation() + ": ");
      System.out.println("Enter Temperature");
      Temp = Input.nextDouble();
     }
    }
    
  3. # 3 楼答案

    把卷曲的背带系在你的身体上

    public void Post() 
    {
         double Temp = 0;
         int K;
         for(K = 0; K < Count ; K++) {
             System.out.print(" " + List[K].GetLocation() + ": ");
             System.out.println("Enter Temperature");
             Temp = Input.nextDouble();
         }
    }
    

    顺便说一句,你应该读standard Java conventions