简体   繁体   English

Java基本数组问题

[英]java basic array question

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

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. 在以后的方法中,我想遍历每个位置并分配一个唯一的Temp。

       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();

}

This simply dumps out all the locations and after that it prompts for a temperature and accepts it. 这只是转储所有位置,然后提示输入温度并接受温度。 But the temperature is not even assigned to the last value in the array. 但是温度甚至没有分配给数组中的最后一个值。

Put curly braces around your for-loop body. 将花括号放在for循环身体上。

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

BTW, you should read the standard Java conventions . 顺便说一句,您应该阅读标准的Java约定

Is this what you would like to do? 这是您想做的吗? Print out each location in the list and request the temperate from the user? 打印出列表中的每个位置并向用户请求温度?

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

Why is Temp initialized in the first line? 为什么在第一行中初始化Temp? Why is there an assignement in the last line? 为什么最后一行有分配? It has no effect. 没有作用。

Why do you declare K outside of the loop? 为什么在循环外声明K? The Codeconventions asks for lowercase letters for attributes, parameters, variables and methods. 代码惯例要求属性,参数,变量和方法的小写字母。

  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. 在以后的方法中,我想遍历每个位置并分配一个唯一的Temp。

Well - you loop through stations, not locations, do you? 好吧-您是在车站而不是地点之间穿行,对吗?

But the temperature is not even assigned to the last value in the array. 但是温度甚至没有分配给数组中的最后一个值。

Which Array? 哪个数组? The Array called List? 数组称为列表? You never assign temp to something, do you? 您永远不会将温度分配给某些东西,是吗?

Do you have a setter in Station? 您在车站有二传手吗? Is it a writable attribute? 它是可写属性吗?

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

might help. 可能有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM