![](/img/trans.png)
[英]The first user input(number) in the 2d array should be less than or equal to other element in the 2d array
[英]how would i loop through an array to see if the user input variable(number) is equal to a element in the array and replace that element with a “@”
//my array(arrayAlphabetizer) is an int[] with random int with a capacity = the users name and surname
//allow the user to enter a value from arrayAlphabetizer element to search for
Scanner value = new Scanner(System.in);
System.out.println("please enter a value from the array to search for");
String search = input.nextLine();
//change input into int
int number = Integer.parseInt(search);
//loop through the array to find the value
for (int i = number; i <= arrayAlphabetizer.length; number++) {
if (i <= number) {
arrayAlphabetizer[i] = 0;
System.out.println(Arrays.toString(arrayAlphabetizer));
} else {
System.out.print(i);
}
}
我已經嘗試過這種方法和其他多種方法,但沒有任何效果
您在循環開始時將 i = 設置為編號。 您的 if 語句將在循環的第一次迭代中執行。 我想你想從 i = 0 開始你的循環。另外我需要小於數組的長度,不小於或等於。
for(int i = 0; i < array.length; i++){
if(array[i] == number)
System.out.println(**whatever you want to print**);
else
System.out.println(**whatever you want to print);
}
您正在嘗試將 char 添加到不起作用的 int 數組中。 你確定你需要這樣做嗎?
我希望這有幫助。 你的問題有點不清楚,我還不能評論要求澄清,所以如果你有問題就回答。
int number = Integer.parseInt(搜索);
for (int i = 0; i <= arrayAlphabetizer.length; number++) {
if (number == arrayAlphabetizer[i])
{
arrayAlphabetizer[i] = 0;
System.out.println(Arrays.toString(arrayAlphabetizer));
} else {
System.out.print(i);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.