簡體   English   中英

Java Scanner 拋出了一些我沒見過的錯誤

[英]Java Scanner is throwing some errors I haven't seen

當我嘗試運行我編寫的這段代碼時:

import java.util.Scanner;
    public class F1Calc
    {
        public static void main(String args[])
        {
            System.out.println("The coefficient of friction of the " + getSurface() + " surface is " + getCoeff() + ".  The mass of the ball was " + getMass() + " and the momentum of the ball was "+ getMomentum() + " Nm.  The potential energy contained by the ball at the top of the .121 m high ramp was " + getPotEnergy() + " Joules.");
        }
        public static String getSurface()
        {
            Scanner kb = new Scanner(System.in);
            System.out.println("What is the surface you are rolling the ball into?");
            String surface = kb.nextLine();
            kb.close();
            return surface;
        }
        public static double getMass()
        {
            Scanner kb2 = new Scanner(System.in);
            System.out.println("What is the mass of the ball in kilograms?");
            double mass = kb2.nextInt();
            kb2.close();
            return mass;
        }
        public static double getPotEnergy()
        {
            double potEnergy = getMass() * .121 * 9.8;
            return potEnergy;
        }
        public static double getMomentum()
        {
            double doublePE = 2 * getPotEnergy();
            double doublePEOverMass = doublePE / getMass();
            double velocity = Math.sqrt(doublePEOverMass);
            double momentum = velocity * getMass();
            return momentum;
        }
        public static double getCoeff()
        {
            double coeff = getPotEnergy() / (getMass() * 9.8);
            return coeff;
        }
    }

控制台顯示如下:

   What is the surface you are rolling the ball into?
    Tile
    What is the mass of the ball in kilograms?
    Exception in thread "main" java.util.NoSuchElementException
        at java.util.Scanner.throwFor(Unknown Source)
        at java.util.Scanner.next(Unknown Source)
        at java.util.Scanner.nextInt(Unknown Source)
        at java.util.Scanner.nextInt(Unknown Source)
        at F1Calc.getMass(F1Calc.java:20)
        at F1Calc.getPotEnergy(F1Calc.java:26)
        at F1Calc.getCoeff(F1Calc.java:39)
        at F1Calc.main(F1Calc.java:6)

添加行以便可以提交我的帖子。 添加行以便可以提交我的帖子。 添加行以便可以提交我的帖子。 添加行以便可以提交我的帖子。

只有一個底層InputStream用於 Scanner,所以不要關閉它 - 這樣做會關閉該類的所有未來實例,從而無法從源中讀取

您不想每次都打開新掃描儀並關閉它。 相反,只需創建一次。 我還修復了您代碼中的另外兩個錯誤。

  1. 您正在使用nextInt()讀取double 這已被替換為nextDouble()
  2. 您要求用戶多次輸入mass 這已經通過緩存mass的值來修復,因此它只從用戶那里讀取一次。

這是更正后的代碼。

import java.util.Scanner;
public class F1Calc
{
    // Cache the values so we don't have to keep bugging the user
    static double mass;

    static Scanner kb;
    public static void main(String args[])
    {
        mass = -1;
        kb = new Scanner(System.in);
        System.out.println("The coefficient of friction of the " + getSurface() + " surface is " + getCoeff() + ".  The mass of the ball was " + getMass() + " and the momentum of the ball was "+ getMomentum() + " Nm.  The potential energy contained by the ball at the top of the .121 m high ramp was " + getPotEnergy() + " Joules.");
    }
    public static String getSurface()
    {
        System.out.println("What is the surface you are rolling the ball into?");
        String surface = kb.nextLine();
        return surface;
    }
    public static double getMass()
    {
        if (mass > 0) return mass;

        System.out.println("What is the mass of the ball in kilograms?");
        mass = kb.nextDouble();
        return mass;
    }
    public static double getPotEnergy()
    {
        double potEnergy = getMass() * .121 * 9.8;
        return potEnergy;
    }
    public static double getMomentum()
    {
        double doublePE = 2 * getPotEnergy();
        double doublePEOverMass = doublePE / getMass();
        double velocity = Math.sqrt(doublePEOverMass);
        double momentum = velocity * getMass();
        return momentum;
    }
    public static double getCoeff()
    {
        double coeff = getPotEnergy() / (getMass() * 9.8);
        return coeff;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM