簡體   English   中英

將字符串添加到掃描儀中

[英]Adding a String into a scanner

//我想在控制台中插入一個十進制數(如 1.85)到 String groesse,我還希望它在 bmi 等式中。 我怎么做?

public static void main(String[] args) {
    Locale.setDefault(Locale.US);
    PrintWriter out = new PrintWriter(System.out);

bmiRechner(out);

out.flush();
}

public static void bmiRechner(PrintWriter out) {
    Scanner sc = new Scanner (System.in);
    
    String = groesse;
    
    out.println("Schönen Guten Tag");
    out.flush();
    out.println("Geben Sie ihre Körpergröße in Metern an");
    out.flush();
    groesse = sc.next();
    out.println("Geben Sie nun ihr Körpergewicht in Kilogramm ein");
    out.flush();
    int gewicht = sc.nextInt();
    
    double bmi = gewicht / Math.pow(groesse, 2);
    
    out.println("Das ist Ihr BMI: " + bmi);
    
}

}

我怎么做?

好的,所以您想從控制台讀取用戶輸入。 這樣做,而不是...

groesse = sc.next();

// This line will get the input line 
groesse = sc.nextLine().trim();

// This will get the numeric value of the input
double groesse = Double.valueOf(groesse)

這同樣適用於其他測量。

暫無
暫無

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

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