Java数组如何将其保存在数组中
如何将其实际保存在数组中? 使用此代码,它不会在数组中保存任何内容 我希望你能告诉我更多的方法,并详细解释,非常感谢Java数组如何将其保存在数组中,java,arrays,Java,Arrays,如何将其实际保存在数组中? 使用此代码,它不会在数组中保存任何内容 我希望你能告诉我更多的方法,并详细解释,非常感谢 import java.util.Scanner; public class CountArray { public static void main(String[] arg) { Scanner scan = new Scanner(System.in); int countPOZ = 0; int countP
import java.util.Scanner;
public class CountArray
{
public static void main(String[] arg)
{
Scanner scan = new Scanner(System.in);
int countPOZ = 0;
int countP5 = 0;
int countNONE = 0;
System.out.println();
System.out.print("Type elements: ");
int[] x = new int [scan.nextInt()];
for(int i = 0; i < x.length; i++)
{
System.out.print("Type numbers: ");
int numrat = scan.nextInt();
if(numrat > 0)
countPOZ++;
else if (numrat % 5 == 0)
countP5++;
else
countNONE++;
}
System.out.println();
System.out.println(x[1]); //here it will display 0 because nothing is saved.. in the array
System.out.println("Positive: "+countPOZ);
System.out.println("Div.. with 5: "+countP5);
System.out.println("Others: "+countNONE);
}
}
import java.util.Scanner;
公共类计数数组
{
公共静态void main(字符串[]arg)
{
扫描仪扫描=新扫描仪(System.in);
int countPOZ=0;
int countP5=0;
int countNONE=0;
System.out.println();
系统输出打印(“类型元素:”);
int[]x=新的int[scan.nextInt()];
对于(int i=0;i
将值存储在x
数组的i
第th个位置,其中包含:
x[i] = someValue;
在循环的上下文中:
for(int i = 0; i < x.length; i++)
{
System.out.print("Type numbers: ");
int numrat = scan.nextInt();
if(numrat > 0)
countPOZ++;
else if (numrat % 5 == 0)
countP5++;
else
countNONE++;
x[i] = numrat;
}
for(int i=0;i
按顺序存储用户的输入
x[i] = scan.nextInt();
移除本地
numrat
并使用x[i]
。请用牙套之类的东西
x[i] = scan.nextInt();
if(x[i] > 0) {
countPOZ++;
} else if (x[i] % 5 == 0) {
countP5++;
} else {
countNONE++;
}
这将在中介绍,并提供一个示例
数组的组件由数组访问表达式()访问,该表达式由一个值为数组引用的表达式组成,后跟一个由[and]括起的索引表达式,如在[i]中
所有数组都是0原点。长度为n的数组可以由0到n-1的整数索引
class Gauss {
public static void main(String[] args) {
int[] ia = new int[101];
for (int i = 0; i < ia.length; i++) ia[i] = i;
int sum = 0;
for (int e : ia) sum += e;
System.out.println(sum);
}
}
高斯类{
公共静态void main(字符串[]args){
int[]ia=新的int[101];
对于(inti=0;i
在代码中
int[]x=新的int[scan.nextInt()]
您正在定义数组的大小。这里没有存储任何元素
因此,定义数组中的任何元素,您必须访问它的索引并将您的值存储在该特定索引中
x[1] = scan.nextInt()
可以将值存储在特定索引中,请尝试在数组中添加值,然后将数组用于业务逻辑
x[1] = scan.nextInt()
int[] x = new int [scan.nextInt()];
for(int i = 0; i < x.length; i++){
System.out.print("Type numbers: ");
x[i] = scan.nextInt();
if(x[i] > 0)
countPOZ++;
else if (x[i] % 5 == 0)
countP5++;
else
countNONE++;
}
int[]x=newint[scan.nextInt()];
对于(int i=0;i0)
countPOZ++;
else如果(x[i]%5==0)
countP5++;
其他的
countNONE++;
}
但是您缺少了一些内容int[]x=new int[scan.nextInt()]
根据您的代码,您不能只传递一个元素并将该元素添加到数组中
我协助将多个元素作为逗号分隔列表
1,2,3,4,5,6,7
传递,然后在代码中创建数组列表int x[]=scan.nextInt().split(“,”
),然后使用它。谢谢。你能详细解释一下吗?@user1873251添加了相关的JL。我知道这个概念,但我知道我在理解为什么I在数组中时遇到了问题[]。。我刚刚计算的值是1。。和2。。直到输入的元素数。.再次阅读包含数组引用的段落,后跟由[
和]
括起的索引表达式。好的,我知道它会扫描,并且位置是由I:]引用的。该行应该给出什么样的异常?int数组的默认值是0Well,那么你的描述是错误的。它与是否在该数组中存储数据无关,您只需输入0
或1
作为数组大小。