Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单java输入字符串_Java_Input_Flow - Fatal编程技术网

简单java输入字符串

简单java输入字符串,java,input,flow,Java,Input,Flow,问题1:当我们把它换到我标记为(2)的其他地方时,上面这一行可以正常工作(1)。 //但在执行“请输入您的年龄”时,行首先出现,尽管在案例(1)中我在该语句之前创建了bufferedreader对象。我希望编译器应该等待用户输入,但它会打印该语句。尽管我在尝试之前创建了ageInput import java.io.*; public class ManyTickets { public static void main (String [] args) throws IOExcept

问题1:当我们把它换到我标记为(2)的其他地方时,上面这一行可以正常工作(1)。 //但在执行“请输入您的年龄”时,行首先出现,尽管在案例(1)中我在该语句之前创建了bufferedreader对象。我希望编译器应该等待用户输入,但它会打印该语句。尽管我在尝试之前创建了ageInput

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));
//问题2:当按999执行时,如果我键入999而不给空格,它是执行,它会给出一些输出,但不会退出。如何避免在开始时使用空格,就像我给出的一样,无论是999还是999或999,我都需要相同的输出。我需要退出,但不需要输入,如99 9;我需要一个方法来避免输入中的空白(开始)

{
如果(输入小于0 | |输入大于110)
输入无效,或者您的年龄似乎太大,无法输入购买
门票);

else if(intInput只需使用Trim函数删除输入前后的空格即可

{
     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()
方法的定义

ageInput.readLine().trim()
公共字符串修剪(){
int len=value.length;
int st=0;
char[]val=value;/*避免getfield操作码*/
而((st
要避免空白,只需从字符串中删除它们,如下所示:

while ((st < len) && (val[len - 1] <= ' ')) {
    len--;
}
问题1:当我们将上面的行更改为我标记为(2)的其他位置时,上面的行工作正常(1)。//但是在执行“请输入您的年龄”时,行首先出现,尽管在案例(1)中我在该语句之前创建了bufferedreader对象。我希望编译器应该等待用户输入,但它会打印该语句。尽管我在尝试之前创建了ageInput

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行之后请求用户输入,即在print语句之后,因此它在询问问题之后等待。如果您将语句
userInput=ageInput.readLine();
移动到print语句之前(将缓冲读取器的实例化保留在第1行),则它将等待用户输入,然后打印语句

答复#2:

您可以使用字符串的
trim()
,如下所示:

userInput = ageInput.readLine().replaceAll(" ","");
intInput = Integer.parseInt (userInput);
如果您需要用户定义的函数(我将避免),您可以执行以下操作:

userInput = ageInput.readLine().trim();
公共字符串myStringTrimmer(){
int len=value.length;
int st=0;
char[]val=userInput.toCharArray();

虽然((stuserInput = ageInput.readLine().trim();
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;
}