[英]Simple java input Strings
import java.io.*;
public class ManyTickets
{
public static void main (String [] args) throws IOException
{
String userInput;
String userInput2;
int intInput = 0;
int intInput2 = 0;
double total = 0.00;
//(1) BufferedReader ageInput = new BufferedReader (new InputStreamReader (System.in));
問題 1:當我們將它更改為我標記為 (2) 的其他地方時,上面的行工作正常 (1)。 // 但在執行“請輸入您的年齡”行時,雖然我在該語句之前創建了 bufferedreader object 以防萬一 (1)。我希望編譯器應該等待用戶輸入,但它會打印該語句。 雖然我在嘗試之前創建了 ageInput。
try{
System.out.println("Please enter your age, or press '999' to exit.");
//(2) BufferedReader ageInput = new BufferedReader (new InputStreamReader (System.in));
userInput = ageInput.readLine();
intInput = Integer.parseInt (userInput);
while (intInput != 999)
//問題 2:如果我在不給空間的情況下鍵入 999 執行到 999,它會執行它會給出一些 output 但不會退出。如何在開頭避免空格,就像我給出的任何內容一樣,無論是 999 還是 999 或 999 我都需要相同的 output。我需要退出但不是像 99 9 這樣的輸入; 我需要避免輸入中出現空格的方法(開頭)。
{
if (intInput < 0 || intInput > 110)
System.out.println("Invalid entry, or your age seems a bit too high to enter buy
tickets);
else if (intInput <= 12)
{
total = total + 6;
System.out.println("The ticket cost for 1 ticket is " + total);
}
else if (intInput <= 64)
{
total = total + 11;
System.out.println("The ticket cost for 1 ticket is " + total);
}
else
{
total = total + 8;
System.out.println("The ticket cost for 1 ticket is $" + total);
}
System.out.println("So far, your tickets cost is: $" + total );
System.out.print("Would you like to buy more tickets? You can buy up to 1 more ticket per customer! If no press 999to exit");
userInput = ageInput.readLine();
intInput2 = Integer.parseInt (userInput);
}
}catch (NumberFormatException e){
System.out.println("Please restart the program, and enter an integer instead!");
}
}
{
double total = 0.0;
System.out.println("Thank you, The total cost for the ticket is: $" + total);
System.out.println("Have a nice day!");
}
}
只需使用 Trim function 即可刪除輸入前后的空格
ageInput.readLine().trim()
這是trim()
方法的定義
public String trim() {
int len = value.length;
int st = 0;
char[] val = value; /* avoid getfield opcode */
while ((st < len) && (val[st] <= ' ')) {
st++;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
如果您只想刪除第一部分,請刪除第二部分
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
對於問題 2, readline()
負責獲取輸入。 您可以在readline
之前的任何位置創建buffered
reader
object
問題 2:執行 999 時,如果我鍵入 999 而沒有給出空格,則執行時會給出一些 output。如何在開頭避免空格,就像我給出的 999 或 999 一樣,我需要相同的 output。我需要方法來避免空格中的空格輸入。
為了避免空格,只需將它們從字符串中刪除,如下所示:
userInput = ageInput.readLine().replaceAll(" ","");
intInput = Integer.parseInt (userInput);
問題 1:當我們將它更改為我標記為 (2) 的其他地方時,上面的行工作正常 (1)。 // 但在執行“請輸入您的年齡”行時,雖然我在該語句之前創建了 bufferedreader object 以防萬一 (1)。我希望編譯器應該等待用戶輸入,但它會打印該語句。 雖然我在嘗試之前創建了 ageInput。
這太曖昧了。
答案#1:
您在第 2 行之后要求用戶輸入,即在 print 語句之后,因此它在提問之后等待。 如果你移動語句userInput = ageInput.readLine();
在打印語句之前(將 Buffered reader 的實例化保留在第 1 行)然后它將等待用戶輸入然后打印語句。
答案#2:
您可以使用 String 的trim()
方法,如下所示:
userInput = ageInput.readLine().trim();
如果您需要用戶定義的 function(我會避免),您可以執行以下操作:
public String myStringTrimmer() {
int len = value.length;
int st = 0;
char[] val = userInput.toCharArray();
while ((st < len) && (val[st] <= ' ')) {
st++;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < value.length)) ? userInput.substring(st, len) : userInput;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.