Java 从ForLoop调用数组
我正在学习java,我需要帮助在For循环中调用数组,这是我的数组代码。 请求了完整的代码,如果它的编码太草率或不正确,请道歉Java 从ForLoop调用数组,java,arrays,Java,Arrays,我正在学习java,我需要帮助在For循环中调用数组,这是我的数组代码。 请求了完整的代码,如果它的编码太草率或不正确,请道歉 import java.util.Scanner ; public class jakeGrim { public static void main(String[] args) { // Local variable int option;
import java.util.Scanner ;
public class jakeGrim {
public static void main(String[] args) {
// Local variable
int option;
String squareFootage="";
int noBed = 0;
double totalSum =0;
String propertyCode="";
String propertyType="";
Scanner input = new Scanner( System.in );
Scanner user_input = new Scanner( System.in );
do{
// Display menu graphics
System.out.println(" ");
System.out.println("| *****Rental Menu******* |");
System.out.println("| 1. Enter rental property Details ");
System.out.println("| 2. Enter monthly rent ( 12 Months ) ");
System.out.println("| 3. Display Annual Rent");
System.out.println("| 4. Display rental report ");
System.out.println("| 5. Display Monthly rents falling below a certain threshold ");
System.out.println(" ");
System.out.println(" Please Select an option: ");
option = input.nextInt();
{
switch (option) {
case 1:
System.out.println("Enter Rental Details: ");
System.out.println("Property Code: ");
propertyCode = user_input.next();
System.out.println("Property Type: ");
propertyType = user_input.next();
System.out.println("Square Footage: ");
squareFootage = user_input.next();
System.out.println("Number Of bedrooms ");
noBed = input.nextInt();
break;
case 2:
{
Scanner keyboardScanner = new Scanner(System.in);
double[] array = new double[12];
for (int i = 0; i < 12; i++) {
System.out.println("Enter Rental for month[" +( i +1)+ "]");
array[i] = keyboardScanner.nextDouble();
}
//So now, we need to do something with that array and sum up all the values in that array.
for (int i = 0; i < array.length; i++){
System.out.println(array[i]);
totalSum += array[i];
}
}
break;
case 3:
System.out.println("The annual rent for propery code "+propertyCode+" is: " + (totalSum));
break;
case 4:
System.out.println(" Property Code: "+propertyCode);
System.out.println(" Property Type: "+propertyType);
System.out.println(" Square Footage: "+squareFootage);
System.out.println(" Number of Bedrooms: "+noBed);
System.out.println("");
System.out.println("");
for(int i = 0; i<12; i++)
System.out.println("Rental for month " + (i+1) + " : " + array[i]);
break;
default:
System.out.println("Invalid selection");
break;
}
}
}while (option!=0);
}
}
但是它说“找不到符号-方法i(int)”
我只想申报,有什么办法吗?试试这个-->
这将打印每个月的租金
for(int i = 0; i < 12; i++)
System.out.println("Rental for month " + (i+1) + " : " + array[i]);
for(int i=0;i<12;i++)
System.out.println(“月租金”+(i+1)+“:“+array[i]);
应在代码中再进行一次编辑
double[] array = new double[12];
for (int i = 0; i < 12; i++)
{
System.out.println( "Enter Rental for month[" + (i+1) + "]" );
array[i] = keyboardScanner.nextDouble();
}
double[]数组=新的double[12];
对于(int i=0;i<12;i++)
{
System.out.println(“输入月份租金[”+(i+1)+“]);
数组[i]=keyboardScanner.nextDouble();
}
这里的i
是什么?看起来像是一种你没有的方法。您必须在循环中执行以下操作
System.out.println("Rental for month " + (i+1) + " : " + array[i]);
似乎您正试图索引到循环变量中,一个整数和一个常数,而此时您应该使用循环计数器索引到数组中(保持不变)。@CosmicComputer您能以应答形式编写它,并说明如何操作吗?在这一点上,我在代码方面是非常无用的:3它说找不到变量数组,我已经有了一个数组。我需要做的就是打印已经生成的数组,用户基本上会“填充”它。我不希望他们一键入就打印它。。它在一个单独的菜单选项中被调用,在switch语句中是另一种情况。@JakeGrim:Ohh。。那么你想在你的开关箱里装什么呢?用户会明确输入他需要租金的月份吗?不会,他们会直接输入他们在12个不同月份支付的租金,这已经分类,我希望调用他们输入的12个数字,并将其放在不同的声明中。基本上是对他们键入的内容进行概述。@JakeGrim:好的。。我弄糊涂了。。我写的代码不是这样做的吗?我的代码将打印他们在12个月内输入/支付的租金。似乎找不到变量数组,i将是数组的名称。
System.out.println("Rental for month 1: "+i(1));
System.out.println("Rental for month " + (i+1) + " : " + array[i]);