繁体   English   中英

简单的Java程序

[英]Simple Java program

我正在编写程序,遇到运行时问题。 该程序可以运行,但是,在我选择了电影并对其评分之后,应该给出平均评分,然后回到开头,让用户选择另一部电影。 而是使用默认值(其他语句)。 之后,它让用户选择另一部电影。 我曾尝试多次重写它,但是它仍然不断显示此问题,我在做什么错? 我该如何解决?

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();
    }
}

问题是nextDouble()将读取一个数字,但不读取其后的换行符。 例如,在阅读评级后,可以添加对nextLine()的调用,或者将nextDouble()替换为Double.parseDouble(s1.nextLine())

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM