久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java Scanner 類中的字符串格式

String formatting in Java Scanner class(Java Scanner 類中的字符串格式)
本文介紹了Java Scanner 類中的字符串格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..
Scanner i=new Scanner(System.in);

System.out.println("Enter an integer: ");
int in=i.nextInt();

System.out.println("Enter an floating point number: ");
double d=i.nextDouble();

System.out.println("Enter a string: ");
String str=i.next();

System.out.printf("%s%n,Sum of%2d and %.2f is %.2f%n",str,in ,d,in+d);

我的問題是格式化我通過掃描儀輸入的字符串.我試圖輸入結果是",但 printf() 似乎只看到字符串的結果"部分,那么空格的命令是什么?謝謝

My problem is with formatting the String I enter through Scanner. I was trying to enter "Result is", but printf() seems to see only the "Result" part of string, so what is the command for blank space? thx

推薦答案

有幾種可能的解決方案,但我相信以下內容會為您提供與其他輸入一致的行為:

There are several possible solutions, but I believe the following will give you consistent behavior with the other inputs:

System.out.println("Enter an floating point number: ");
double d = i.nextDouble();
i.skip("((?<!\R)\s)*"); // skip whitespace, stopping after any newline

System.out.println("Enter a string: ");
String str = i.nextLine();

如果需要,這種方法可以讓您在一行中輸入所有輸入.

This approach would allow you to enter all the inputs on a single line, if so desired.

例如:

1 1.2 結果是

但是,如果您真的打算讓您的用戶在每次輸入后按 Enter,那么使用 Scanner 的 nextLine() 方法讀取所有輸入,然后根據需要進行解析(使用 Integer.parseInt 等).

However if you really intend for your users to press Enter after every input, then it would be most consistent to read all the inputs with Scanner's nextLine() method, then parse as needed (using Integer.parseInt, etc).

Java 9

由于 Java 9 中的一個錯誤,必須在換行符匹配器 R 周圍添加原子分組 (?> ... ).有關詳細信息,請參閱錯誤報告 JDK-8176983.

Due to a bug in Java 9, the atomic grouping (?> ... ) must be added around the linebreak matcher R. See bug report JDK-8176983 for details.

i.skip("((?<!(?>\R))\s)*"); // skip whitespace, stopping after any newline
                              // Compatibility Note: Java 9 safe use of R

如果用于 Java 8,此代碼也可以正常工作并且不會導致任何問題,因此實際上我建議您在代碼中使用此解決方法版本,只是為了安全起見(例如,如果有人可能復制/粘貼或設置目標到不同的 JDK).

This code will also work fine and not cause any problems if used for Java 8, so actually I recommend you use this workaround version in your code just to be on the safe side (e.g. if someone may copy/paste or set target to a different JDK).

Java 7 及更早版本

換行符匹配器 R 在 Java-8 或更高版本中可用.在該版本之前,您必須使用等效"模式 u000Du000A|[u000Au000Bu000Cu000Du0085u2028u2029] 但是作為真正的等效模式它實際上必須包含在原子分組 (?> ... ) 中.有關詳細信息,請參閱文檔錯誤報告 JDK-8176029.p>

The linebreak matcher R is available in Java-8 or later. Prior to that version, you would have to use the "equivalent" pattern u000Du000A|[u000Au000Bu000Cu000Du0085u2028u2029] however to work as a true equivalent it actually must be wrapped in the atomic grouping (?> ... ). See documentation bug report JDK-8176029 for details.

i.skip("((?<!(?>\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]))\s)*"); // skip whitespace, stopping after any newline

這篇關于Java Scanner 類中的字符串格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 久久久久国产精品一区二区 | 毛片一级网站 | 欧美黑人体内she精在线观看 | 中文字幕国产视频 | 国产精品国产a级 | 激情五月综合 | 成人国产精品久久久 | 伊人久久精品一区二区三区 | 中文字幕亚洲一区 | 久热久热 | 岛国av在线免费观看 | 亚洲在线一区 | 精品免费国产视频 | 国产精品免费高清 | 正在播放一区二区 | caoporn视频| 国产精品免费高清 | 欧美视频成人 | 中文字幕一区二区三区精彩视频 | 草草影院ccyy | 视频第一区 | 99国产精品99久久久久久 | 人干人操 | 蜜桃av一区二区三区 | 99久久久国产精品免费消防器 | 久久综合伊人一区二区三 | 国产欧美日韩一区二区三区 | 国产在线精品一区二区三区 | www.久久99| 亚洲成年在线 | 美女爽到呻吟久久久久 | 羞羞的视频在线 | 成人欧美一区二区三区白人 | www.久草.com | 亚洲综合大片69999 | 热99| 色网站在线免费观看 | 成人久久一区 | 亚洲 成人 av | 久久久蜜臀国产一区二区 | 天天干天天爱天天爽 |