【发布时间】:2014-08-27 11:04:17
【问题描述】:
我有以下课程
import java.util.Scanner;
public class Album{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("How many songs do your CD contain?");
int songs = sc.nextInt();
String[] songNames = new String[songs];
for (int i = 0; i < songs; i++) {
System.out.println("Please enter song nr " + (i+1) + ": ");
songNames[i] = sc.nextLine();
// What is wrong here? (See result for this line of code)
// It is working when I use "sc.next();"
// but then I can't type a song with 2 or more words.
// Takes every word for a new song name.
}
System.out.println();
System.out.println("Your CD contains:");
System.out.println("=================");
System.out.println();
for (int i = 0; i < songNames.length; i++) {
System.out.println("Song nr " + (i+1) + ": " + songNames[i]);
}
}
}
我无法输入歌曲名称 nr 1,因为它总是同时显示前两个。
如果我输入 3,就像这样:
How many songs do your CD contain?
3
Please enter song nr 1:
Please enter song nr 2:
【问题讨论】:
-
我编辑了你的问题,给它一个更好的标题。当然,你的旧标题给人们一个挑战,让人们做一些代码检测工作来发现你的代码中的问题,它很好地达到了它的目的,有 5 个答案,但是前面的 我的代码有什么问题?检查我的评论行可能适合任意多个问题。
标签: java string java.util.scanner