简体   繁体   中英

Simple Java program

I am working on a program and I encountered a runtime problem. The program works, but, after I choose the movie and rate it, it's supposed to just give the average rating and go back to the beginning and let the user choose another movie. Instead, it goes to the default (else statement). After that, it lets the user choose another movie. I have tried to rewrite it multiple times, but it still keeps showing this issue, what am I doing wrong? How can I fix it?

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

The problem is, that nextDouble() will read a number but not the newline following. Add a call to nextLine() after you read the rating or replace nextDouble() with Double.parseDouble(s1.nextLine()) , for example.

On a different note, aside from the non-existent formatting in your source code, there are a couple of other things wrong with it, most importantly that your program never terminates until it ultimately crashes because you run out of stack space. If this is a school assignment you should probably try to fix that as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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