简单Java程序
我正在编写一个程序,遇到了一个运行时问题。该程序可以运行,但是,在我选择电影并对其进行评级之后,它应该只给出平均评级,然后返回到开头,让用户选择另一部电影。相反,它将转到默认值(else语句)。之后,它允许用户选择另一部电影。我试过多次重写,但它仍然显示出这个问题,我做错了什么?我怎样才能修好它简单Java程序,java,runtime-error,Java,Runtime Error,我正在编写一个程序,遇到了一个运行时问题。该程序可以运行,但是,在我选择电影并对其进行评级之后,它应该只给出平均评级,然后返回到开头,让用户选择另一部电影。相反,它将转到默认值(else语句)。之后,它允许用户选择另一部电影。我试过多次重写,但它仍然显示出这个问题,我做错了什么?我怎样才能修好它 import java.text.DecimalFormat; import java.util.Scanner; public class movie { Scanner s1=new S
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())
另一方面,除了源代码中不存在的格式之外,它还有一些其他问题,最重要的是,程序永远不会终止,直到因为堆栈空间不足而最终崩溃。如果这是一项学校作业,你可能也应该尝试解决这个问题。我会尝试在我获得更多声誉后添加一张发生的事情的图片。你所说的“电话”是什么意思。那是什么?我用Double.parseDouble(s1.nextLine())替换了nextDouble;但这给了我一个错误。