gpt4 book ai didi

Java 不断跳过我的扫描仪对象

转载 作者:行者123 更新时间:2023-12-01 09:50:27 25 4
gpt4 key购买 nike

这让我很困惑。我试图了解如何解决 Java 无法识别出我的“setTitle”方法存在于第一首“歌曲”之后的问题。这是一个音乐应用程序。它还有另外两个类。非常感谢所有帮助。

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)

编辑:添加了歌曲类。

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 不断跳过我的扫描仪对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com