[英]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.