需要帮助弄清楚如何放置一个' q'退出我的计划

时间:2015-11-03 03:41:41

标签: java

这是我到目前为止所做的,但是当我输入q时,它给了我一些文字,这些文字不是我想要的" Q"

import java.util.Scanner;

public class DollarsandCents
{
  public static void main(String[] args)
  {
    Scanner stdIn = new Scanner(System.in);
    String input = "q";
    double currency; 

    System.out.println("Enter a currency value or enter q to quit:");
    currency = stdIn.nextDouble ();

    if (currency >= .00)
    {
      System.out.printf("Formatted currency value: $%,.2f\n", currency);
    }
    else 
    {
      System.out.print("Q");
    }
  } // end main
} // end class DollarsandCents

我需要添加什么才能使其正常工作?

2 个答案:

答案 0 :(得分:2)

import java.util.Scanner;
public class Currency 
{
  public static void main(String args[])
  {
      Scanner stdIn = new Scanner(System.in);
      String currencyValue="";
      while (true)
      {
          System.out.print("Enter a currency value or enter q to quit: ");
          currencyValue = stdIn.nextLine();
          if(currencyValue.equals("q")) {
              System.out.println("You pressed q, have a nice day");
              break;
          }
          System.out.printf("Formatted currency value: $%,.2f\n", Double.parseDouble(currencyValue));
      }
   }
}

答案 1 :(得分:0)

public static void main(String[] args)
  {
    Scanner stdIn = new Scanner(System.in);
    String input = "q";
    double currency; 

    System.out.println("Enter a currency value or enter q to quit:");
    currency = stdIn.nextLine ();

    if (currency.equals(q))
    {
      System.out.print("Q");

    }
    else 
    {
      double curr =  Double.parseDouble(currencyValue);
      if( Double.compare(curr,0.00) >= 0)
      {
          System.out.printf("Formatted currency value: $%,.2f\n", curr);
      }

      else 
           System.out.printf("Currency value is less than 0.00");

    }
  } // end main