簡體   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