[英]how could I loop an array with a scanner input
我怎么能做一個 If 或 else 語句並循環數組包 Songsort 的輸入;
import java.util.Scanner;
public class Songs {
public static void main(String[] args) {
int num = 0;
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int[] songAmount = new int[num];
if () {}
songAmount[0] = sc.nextInt();
}
}
if 條件不能為空。 您是否嘗試編譯代碼? 你會得到這個錯誤:
標記“(”上的語法錯誤,此標記后應有表達式
除此之外,您掃描輸入的整數值並將其存儲在songAmount[0]
。 您將覆蓋songAmount[0]
處的值,直到您的掃描儀完成。 您要使用的是 for 循環或 while 循環:
for(int i = 0; i < num; i++) {
songAmount[i] = sc.nextInt();
}
但是,您的num
似乎為零,那么您的代碼的目的是什么? 您是否試圖保存與掃描儀讀取的一樣多的整數? 如果是這樣,您應該改用List
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.