Java 一直跳过我的扫描仪对象
Java keeps skipping over my scanner objects
这让我很费解。我试图了解如何解决 Java 无法识别我的 "setTitle" 方法存在于第一个 "Song." This is a music application program 之后的问题。它还有另外两个 classes。非常感谢所有帮助。
import java.util.*;
public class MusicApp
{
static Scanner keyboard = new Scanner(System.in);
static Song s1 = new Song();
static Song s2 = new Song();
static Song s3 = new Song();
static Album a1 = new Album();
public static Song song1(Song s1)
{
System.out.println("Song One");
System.out.println("Enter the title of a song: ");
s1.setTitle(keyboard.nextLine());
System.out.println("Enter the artist's name: ");
s1.setArtist(keyboard.nextLine());
System.out.println("Enter the length of the song in minutes: ");
s1.setMinutes(keyboard.nextInt());
a1.add(s1);
return s1;
}
public static Song song2(Song s2)
{
System.out.println("Song Two");
System.out.println("Enter the title of a song: ");
s2.setTitle(keyboard.nextLine());
System.out.println("Enter the artist's name: ");
s2.setArtist(keyboard.nextLine());
System.out.println("Enter the length of the song in minutes: ");
s2.setMinutes(keyboard.nextInt());
a1.add(s2);
return s2;
}
public static Song song3(Song s3)
{
System.out.println("Song Two");
System.out.println("Enter the title of a song: ");
s3.setTitle(keyboard.nextLine());
System.out.println("Enter the artist's name: ");
s3.setArtist(keyboard.nextLine());
System.out.println("Enter the length of the song in minutes: ");
s3.setMinutes(keyboard.nextInt());
a1.add(s3);
return s3;
}
public static void main(String[] args)
{
song1(s1);
System.out.println("");
song2(s2);
System.out.println("");
song3(s3);
System.out.println("Enter the title of a song in the album: ");
Song songInput = a1.getTitle(keyboard.nextLine());
System.out.println(songInput);
System.out.println(a1.toString());
}
}
编译器做了一些有趣的事情,比如跳过某些字段。请参阅下面的编译器输出:
Song One
Enter the title of a song:
Three Little Birds
Enter the artist's name:
Bob Marley
Enter the length of the song in minutes:
5
Song Two
Enter the title of a song:
Enter the artist's name:
Very Best
Enter the length of the song in minutes:
Ash
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at MusicApp.song2(MusicApp.java:42)
at MusicApp.main(MusicApp.java:64)
编辑:
添加歌曲 class.
public class Song
{
private String artist = "";
private String title = "";
private int minutes = 0;
public Song()
{
}
public String getArtist()
{
return artist;
}
public void setArtist(String singer)
{
artist = singer;
}
public String getTitle()
{
return title;
}
public void setTitle(String name)
{
title = name;
}
public int getMinutes()
{
return minutes;
}
public void setMinutes (int mins)
{
minutes = mins;
}
public String toString()
{
return getTitle() + " by " + getArtist() + " is " + minutes + " minutes long";
}
}
尝试在每次调用 'nextInt()'
后放置它
keyboard.nextLine()
基本上它会跳过一个输入,因为 nextInt 不会捕捉到“\n”。
你可以看到更好的解释here
这让我很费解。我试图了解如何解决 Java 无法识别我的 "setTitle" 方法存在于第一个 "Song." This is a music application program 之后的问题。它还有另外两个 classes。非常感谢所有帮助。
import java.util.*;
public class MusicApp
{
static Scanner keyboard = new Scanner(System.in);
static Song s1 = new Song();
static Song s2 = new Song();
static Song s3 = new Song();
static Album a1 = new Album();
public static Song song1(Song s1)
{
System.out.println("Song One");
System.out.println("Enter the title of a song: ");
s1.setTitle(keyboard.nextLine());
System.out.println("Enter the artist's name: ");
s1.setArtist(keyboard.nextLine());
System.out.println("Enter the length of the song in minutes: ");
s1.setMinutes(keyboard.nextInt());
a1.add(s1);
return s1;
}
public static Song song2(Song s2)
{
System.out.println("Song Two");
System.out.println("Enter the title of a song: ");
s2.setTitle(keyboard.nextLine());
System.out.println("Enter the artist's name: ");
s2.setArtist(keyboard.nextLine());
System.out.println("Enter the length of the song in minutes: ");
s2.setMinutes(keyboard.nextInt());
a1.add(s2);
return s2;
}
public static Song song3(Song s3)
{
System.out.println("Song Two");
System.out.println("Enter the title of a song: ");
s3.setTitle(keyboard.nextLine());
System.out.println("Enter the artist's name: ");
s3.setArtist(keyboard.nextLine());
System.out.println("Enter the length of the song in minutes: ");
s3.setMinutes(keyboard.nextInt());
a1.add(s3);
return s3;
}
public static void main(String[] args)
{
song1(s1);
System.out.println("");
song2(s2);
System.out.println("");
song3(s3);
System.out.println("Enter the title of a song in the album: ");
Song songInput = a1.getTitle(keyboard.nextLine());
System.out.println(songInput);
System.out.println(a1.toString());
}
}
编译器做了一些有趣的事情,比如跳过某些字段。请参阅下面的编译器输出:
Song One
Enter the title of a song:
Three Little Birds
Enter the artist's name:
Bob Marley
Enter the length of the song in minutes:
5
Song Two
Enter the title of a song:
Enter the artist's name:
Very Best
Enter the length of the song in minutes:
Ash
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at MusicApp.song2(MusicApp.java:42)
at MusicApp.main(MusicApp.java:64)
编辑: 添加歌曲 class.
public class Song
{
private String artist = "";
private String title = "";
private int minutes = 0;
public Song()
{
}
public String getArtist()
{
return artist;
}
public void setArtist(String singer)
{
artist = singer;
}
public String getTitle()
{
return title;
}
public void setTitle(String name)
{
title = name;
}
public int getMinutes()
{
return minutes;
}
public void setMinutes (int mins)
{
minutes = mins;
}
public String toString()
{
return getTitle() + " by " + getArtist() + " is " + minutes + " minutes long";
}
}
尝试在每次调用 'nextInt()'
后放置它keyboard.nextLine()
基本上它会跳过一个输入,因为 nextInt 不会捕捉到“\n”。 你可以看到更好的解释here