简体   繁体   English

需要帮助弄清楚如何在我的程序中添加“ q”退出

[英]need help figuring out how to put a 'q' quit into my program

this is what i have so far, but when i input q it gives me lines of text that are not my desired out put of "Q" 这是我到目前为止的内容,但是当我输入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

what do i need to add that would be make it work? 我需要添加什么才能使其正常工作?

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));
      }
   }
}
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要帮助弄清楚如何阻止我的程序抛出NullPointerException - I need help figuring out how to stop my program from throwing a NullPointerException 需要帮助找出我的代码中的错误 - Need help figuring it out errors with my code 需要帮助弄清楚为什么我的 TestClock.java 程序出现 java.lang.StackOverflowError - Need help figuring out why I am getting a java.lang.StackOverflowError on my TestClock.java program 需要帮助弄清楚如何确定最大和最小数量 - Need help figuring out how to determine biggest and smallest count 需要帮助找出正确的正则表达式模式 - Need help in figuring out the right regex pattern 需要帮助找出问题所在 - need help figuring out what is wrong with this 我正在我的 java class 中处理数据库,我需要帮助弄清楚如何更新数据库中的特定值/列 - I'm working on databases in my java class, and I need help figuring out how to update a specific value/column in the database 我需要帮助弄清楚为什么清除按钮代码不起作用 - I need help figuring out why my clear button code does not work Java服务器程序在启动时会吐出两个U + FFFD字符。 需要帮助弄清楚它在哪里执行以及为什么执行? - Java server program is spitting out two U+FFFD characters when launched. Need help figuring out where it does this and why? 需要帮助弄清楚如何大写从文本文件中读取的任何小写“i” - Need help figuring out how to capitalize any lower case "i" read from a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM