0

我正在开发一个程序,但遇到了运行时问题。该程序有效,但是,在我选择电影并对其进行评分后,它应该只给出平均评分并返回开始并让用户选择另一部电影。相反,它转到默认值(else 语句)。之后,它让用户选择另一部电影。我已经尝试多次重写它,但它仍然显示这个问题,我做错了什么?我该如何解决?

import java.text.DecimalFormat;
import java.util.Scanner;


public class movie {
    Scanner s1=new Scanner(System.in);
    private String movielist, movie;
    private double userR;
    public String pg="rated PG-13", r="rated R";
    public String rate="Rate this movie 1-5, 1 being terrible and 5 great";
    public String crm1="Score       1   2   3   4   5\n# of Raters 1   3   1   3   12",crm2="Score       1   2   3   4   5\n# of Raters 4   2   4   6   4", crm3="Score       1   2   3   4   5\n# of Raters 3   0   5   5   7";
    DecimalFormat userR1=new DecimalFormat("#.#");


    public String m1(){
        System.out.println("\nChoose one of the following movies\nJurassic Park, Identity Theft, The Dark Night");
        movielist=s1.nextLine();
        if(movielist.equalsIgnoreCase("Jurassic Park"))
        {
            System.out.println("Jurassic Park is "+pg+"\nCritics rated this movie: \n"+crm1+"\n"+rate);
            userR=s1.nextDouble();System.out.println("With your rating, the average rating for this movie is: "+(userR1.format((82+userR)/21)));
        }
        else if(movielist.equalsIgnoreCase("Identity Theft"))
        {
            System.out.printf("Identity Theft is "+r+"\nCritics rated this movie: \n"+crm2+"\n"+rate);
            userR=s1.nextDouble();
            System.out.println("With your rating, the average rating for this movie is: "+(userR1.format((68+userR)/21)));
        }
        else if(movielist.equalsIgnoreCase("The Dark Night"))
        {
            System.out.printf("The Dark Night is "+pg+"\nCritics rated this movie: \n"+crm3+"\n"+rate);
            userR=s1.nextDouble();
            System.out.println("With your rating, the average rating for this movie is: "+(userR1.format((73+userR)/21)));
        }
        else 
        {
            System.out.println("No movie with that name found, make sure your spelling is correct.");
        }
        return m1();
    }
}
4

1 回答 1

0

问题是,它nextDouble()会读取一个数字,而不是后面的换行符。nextLine()例如,在您阅读评分后添加呼叫或nextDouble()替换Double.parseDouble(s1.nextLine())为 。

另一方面,除了源代码中不存在的格式之外,它还有其他一些问题,最重要的是,您的程序永远不会终止,直到由于堆栈空间不足而最终崩溃。如果这是一项学校作业,您可能也应该尝试解决它。

于 2013-04-07T16:29:06.963 回答