所以我有一个问题要为我的一个班级解决。10个人计算到100,条件是如果这个数字可以被7整除,他们会转换方向,意味着第6个人会说6,第7个人会说7,第6个人会说8。如果这个数字可以被11整除,他们就会跳过一个人。所以如果第12个是下一个,他们就去找第13个人。我能够编写和理解这个问题,但是我找不到一种迭代数组索引的方法,所以我可以满足问题询问的内容。下面是我的代码:
package com.company;
public class Main {
public static void main(String[] args) {
String persoane[] = {"Marcus ", "Fane", "Ion ", "Ghita ", "Fanescu ", "Vrabie ", "Ecaterina ", "Marius ", "Florina ", "Lupu "};
int numaratoare = 0;
for (int i=0; i<100; i++){
numaratoare = i;
if (numaratoare%7==0) {
}
if (numaratoare%11==0){
}
}
}
}发布于 2019-10-05 19:52:38
您需要两个计数器来解决此问题,一个用于100个数字,另一个用于人员。此外,您还需要知道哪一个是人民柜台的方向。解决方案可能是这样的:
package com.company;
public class Main {
public static void main(String[] args) {
String persoane[] = {"Marcus", "Fane", "Ion", "Ghita", "Fanescu", "Vrabie", "Ecaterina", "Marius",
"Florina", "Lupu"};
int personCounter = 0;
boolean directionInverse = false;
for (int i = 0; i < 100; i++){
if (i % 7 == 0) {
personCounter--;
directionInverse = true;
}
if (i % 11 == 0){
personCounter++;
directionInverse = false;
}
if (directionInverse) {
personCounter--;
if (personCounter == -1) {
personCounter = 9;
}
} else {
personCounter++;
}
System.out.println(persoane[personCounter % 10] + " says " + (i + 1));
}
}
}发布于 2019-10-05 20:09:32
1-我已经创建了一个用于遍历数组的方向标志。
2-遍历数组的索引。
3-一个回答变量,用来找出当count达到100时,你处于哪个位置。
4-正确调试代码,并仔细尝试理解每一条语句。
5-如果你在代码方面有任何问题,请发表评论。
public static void main(String[] args) {
String persoane[] = { "Marcus ", "Fane", "Ion ", "Ghita ", "Fanescu ", "Vrabie ", "Ecaterina ", "Marius ",
"Florina ", "Lupu " };
int numaratoare = 0;
boolean direction = true;
int answer = 0;
int index = 1;
for (int i = 1; i < 100; i++) {
if (index == 0 && direction == false) {
index = persoane.length - 1;
}
if (index == persoane.length - 1 && direction) {
index = 0;
}
numaratoare = i;
if (numaratoare % 7 == 0) {
if (direction) {
direction = false;
} else {
direction = true;
}
}
if (numaratoare % 11 == 0) {
if (direction) {
if( index == 9) {
index = 0;
}else{
index++;
}
} else {
if( index == 0) {
index = 9;
}else{
index--;
}
}
}
if (i == 99) {
answer = index;
}
if (direction) {
index++;
} else {
index--;
}
}
System.out.println(answer);
}https://stackoverflow.com/questions/58247760
复制相似问题