Java 如何查找数组的段
我需要帮助找到如何分段数组以找到每四个值,并查看这些值是否超过10。我试图实现。子列表,但我不确定这是否正确。下面是我目前为止所做的事情,谢谢你的帮助Java 如何查找数组的段,java,arrays,segment,Java,Arrays,Segment,我需要帮助找到如何分段数组以找到每四个值,并查看这些值是否超过10。我试图实现。子列表,但我不确定这是否正确。下面是我目前为止所做的事情,谢谢你的帮助 import java.util.Scanner; public class Problem1 { public static void main(String [] args){ Scanner sc = new Scanner(System.in); System.out.print( "Enter h
import java.util.Scanner;
public class Problem1 {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.print( "Enter how many downs there are:");
int x = sc.nextInt();
double[] yards = new double [x];
double num1 = 0;
for (int i = 0; i < yards.length; i++){
//System.out.print( (i+1)+ " Enter the quality of the food on a scale from 1-5:");
System.out.print( "Enter the yards they moved:");
yards[i] = sc.nextDouble();
num1 += yards[i];
if (yards[i] <=0){
System.out.print("SAFETY");
}
if ( num1 == 100){
System.out.print("TOUCHDOWN");
System.exit(0);
//System.out.print( num1 );
}
}
}
}
}
import java.util.Scanner;
公共类问题1{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.print(“输入有多少下拉:”);
int x=sc.nextInt();
双人[]码=新双人[x];
双num1=0;
对于(int i=0;ifor(int i=0;i10){
//如果4个数字之和大于10,请采取措施
}否则{
//如果它小于10,就做些什么
}
}否则{
//如果数组中剩余的数字少于4个,请执行某些操作。
//处理数组中的最后3、2或1个数字
}
}
根据我从您的代码和您的要求中理解的内容。for(int I=0;Ifor(int i = 0; i < array.length; i++){
if(i % 4 == 0){
if(array[i] > 10){
//Whatever you want to do here
}
}
}
如果(i%4==0){
if(数组[i]>10){
//你想在这里干什么都行
}
}
}
现在让我在这里解释一切:
- 您可以使用
循环在数组中循环for
- 检查
除以4是否有余数(百分比除以2个数字并输出余数)i
- 如果数字(
)没有余数,则检查i
是否大于10array[i]
- 然后,如果
恰好位于您要查找的“第四个”插槽之一,那么您希望发生的任何事情都会发生array[i]
类似地,您可以说
if(i%4==0&&array[i]>10)
那么您就不会嵌套if
语句。if(码[i]=0)
这有什么用?@Takendark同意无论发生什么情况,这都是真的。@Takendark很抱歉,这只是我无意中添加的东西,结果一文不值。我编辑了代码以删除它。感谢您指出。
for(int i = 0; i < array.length; i++){
if(i % 4 == 0){
if(array[i] > 10){
//Whatever you want to do here
}
}
}