how could I make a If or else statement and loop the input for the array package 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();
}
}
The if condition cannot be empty. Did you try compiling the code? You'll get this error:
Syntax error on token "(", Expression expected after this token
Besides that, you scan the input for an integer value and store it at songAmount[0]
. You'll override the value at songAmount[0]
until your scanner finishes. What you want to use is a for loop or a while loop:
for(int i = 0; i < num; i++) {
songAmount[i] = sc.nextInt();
}
However, your num
seems to be zero, so what's the purpose of your code? Are you trying to save as many integers as your scanner reads? If so, you should use a List
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.