字符串while循环第2部分

时间:2016-02-05 05:11:45

标签: java string while-loop try-catch java.util.scanner

我之前已经问过类似的问题:String while loop,但现在我还有另外一个我无法弄清楚的问题......

double bill = 0.0;
double tip = 0.0;

// Taking an input
System.out.print("Please enter the total amount of your bill > ");
String strBill = scan.nextLine();
boolean validatedBill = false;

  // Validating Bill
  while(!validatedBill) {
    try {
      bill = Double.parseDouble(strBill);
      validatedBill = true;
    } catch(NumberFormatException e) {
      System.out.print("Enter a valid number > ");
      strBill = scan.nextLine();
    } // end of catch block
  } // end of while loop

  while(bill < 0) {
    System.out.print("Your bill amount is less then 0, try again > ");
    bill = scan.nextDouble();
  } // end of while loop

  System.out.print("Please enter the tip percentage > ");
  String strTip = scan.nextLine();
  boolean validatedTip = false;

    // Validating Tip
    while(!validatedTip) {
      try {
        tip = Double.parseDouble(strTip);
        validatedTip = true;
      } catch(NumberFormatException e) {
        System.out.print("Enter a valid tip percentage > ");
        strTip = scan.nextLine();
      } // end of catch block
    } // end of while loop

    while(tip < 0) {
      System.out.print("Your tip is below 0, try again > ");
      tip = scan.nextDouble();
    } // end of while loop

我的输入效果很好,但是当程序要求我输入我的提示百分比时,它会以这种方式显示:

  

请输入提示百分比&gt;输入有效的提示百分比&gt;

所有在一行但同时正常,程序可以验证字符串和数字。

代码应该以这种方式工作: 1.请输入账单总额&gt; 2.请输入提示百分比&gt;

非常感谢!

固定代码

double bill = 0.0;
double tip = 0.0;

// Taking an input
System.out.print("Please enter the total amount of your bill > ");
String strBill = scan.nextLine();
boolean validatedBill = false;

  // Validating Bill
  while(!validatedBill) {
    try {
      bill = Double.parseDouble(strBill);
      validatedBill = true;
    } catch(NumberFormatException e) {
      System.out.print("Enter a valid number > ");
      strBill = scan.nextLine();
    } // end of catch block
  } // end of while loop

  while(bill < 0) {
    System.out.print("Your bill amount is less then 0, try again > ");
    bill = scan.nextDouble();
  } // end of while loop
    scan.nextLine();
  System.out.print("Please enter the tip percentage > ");
  String strTip = scan.nextLine();
  boolean validatedTip = false;

    // Validating Tip
    while(!validatedTip) {
      try {
        tip = Double.parseDouble(strTip);
        validatedTip = true;
      } catch(NumberFormatException e) {
        System.out.print("Enter a valid tip percentage > ");
        strTip = scan.nextLine();
      } // end of catch block
    } // end of while loop

    while(tip < 0 || tip > bill) {
      System.out.print("Your tip is less then 0 or greater then your bill, try again > ");
      tip = scan.nextDouble();
    } // end of while loop

1 个答案:

答案 0 :(得分:1)

你想做什么

System.out.println("Please enter the tip percentage > ");

这会自动在最后添加换行符。

或者,你可以这样做。

System.out.print("Please enter the total amount of your bill > \n");
String strBill = scan.nextLine();
boolean validatedBill = false;

  // Validating Bill
  while(!validatedBill) {
    try {
      bill = Double.parseDouble(strBill);
      validatedBill = true;
    } catch(NumberFormatException e) {
      System.out.print("Enter a valid number > \n");
      strBill = scan.nextLine();
    } // end of catch block
  } // end of while loop

有关System.out.println()

的更多信息

或者您的完整代码:

double bill = 0.0;
double tip = 0.0;

// Taking an input
System.out.println("Please enter the total amount of your bill > ");
String strBill = scan.nextLine();
boolean validatedBill = false;

  // Validating Bill
  while(!validatedBill) {
    try {
      bill = Double.parseDouble(strBill);
      validatedBill = true;
    } catch(NumberFormatException e) {
      System.out.println("Enter a valid number > ");
      strBill = scan.nextLine();
    } // end of catch block
  } // end of while loop

  while(bill < 0) {
    System.out.println("Your bill amount is less then 0, try again > ");
    bill = scan.nextDouble();
  } // end of while loop

  System.out.println("Please enter the tip percentage > ");
  String strTip = scan.nextLine();
  boolean validatedTip = false;

    // Validating Tip
    while(!validatedTip) {
      try {
        tip = Double.parseDouble(strTip);
        validatedTip = true;
      } catch(NumberFormatException e) {
        System.out.println("Enter a valid tip percentage > ");
        strTip = scan.nextLine();
      } // end of catch block
    } // end of while loop

    while(tip < 0) {
      System.out.println("Your tip is below 0, try again > ");
      tip = scan.nextDouble();
    } // end of while loop