【问题标题】:String, Substring, and if-else Statements in Change- Making ProgramChange-making 程序中的字符串、子字符串和 if-else 语句
【发布时间】:2015-11-13 11:27:25
【问题描述】:

我这周刚开始上 Java 课程,我们的第一个项目将于周日到期,但我遇到了一些麻烦。该项目的基本要点是,我们必须创建一个程序来计算购买后返还的零钱,并告诉用户有多少 25 美分硬币和五分硬币构成了该零钱。

问题是老师希望输入的形式是“pay:cost”,这不适合任何原始类型。所以他建议使用字符串和子字符串将“支付”和“成本”与冒号分开,但我不知道该怎么做。 “支付”和“成本”也必须是特定范围内的数字,我认为这就是 if-else 语句的用途。问题是我没有牢牢掌握语法,需要一些帮助来设置它。

这是他为项目逐字给出的说明,以及我现在拥有的代码。请记住,这是宝宝的第一个项目,所以请多多包涵。任何帮助表示赞赏。

说明:

1.) 您必须检查用户输入的数据是否符合要求的语法。如果没有,您的程序必须显示错误对话框并退出。仅当满足以下所有条件时,输入才有效: 

  • 输入的格式为 pay:cost,其中 pay 和 cost 都是整数。 

  • pay 是美元的整数(100、200、300,...)。 

  • 工资小于或等于 900 美分。 

  • pay 大于或等于 cost。 

  • 成本大于或等于 5,并且只能使用 25 美分、硬币和镍表示(即 5、10、15、... 95、100、105、... 890、895 或900)。

2.) 如果输入有效,那么您的程序必须显示一个消息对话框,显示提供正确找零所需的五分钱、一角硬币和四分之一硬币的最小数量,其中零钱 = 工资 - 成本。

3.) 当用户关闭输出消息对话框时,您的程序必须退出。

4.) 你只能导入 javax.swing.JOptionPane

代码:

import javax.swing.JOptionPane;

public class ChangeMakerWindow {
  public static void main(String[] args) {
    String amountString = JOptionPane.showInputDialog("Enter the amount you pay in cents,\n" +
            "followed by the cost of the item in cents.\n" +
            "The input must be in the form: pay:cost,\n" +
            "where pay is a whole number (100, 200, 300, etc) to a maximum of 900,\n" +
            "and cost is  a multiple of five (5, 10, 15, etc) to a maximum of 900");

    int pay, cost, change, originalChange, quarters, dimes, nickles;
    change = pay - cost;
    originalChange = change;

    quarters = change / 25;
    change = change % 25;
    dimes = change / 10;
    change = change %10;
    nickles = change / 5;

    JOptionPane.showMessageDialog(null, originalChange +
            " cents in coins can be given as:\n" +
            quarters + " quarters\n" +
            dimes + " dimes\n" +
            nickles + " nickles\n");
    System.exit(0);
  }
}

【问题讨论】:

  • 我实施了一个基本解决方案来帮助您获得报酬和成本。剩下的就是你的基本逻辑了。

标签: java string if-statement substring


【解决方案1】:

使用 String.split(":") 将 pay 和 cost 两个单独的项目。然后用 Integer.parseInt(String s) 转换成整数。

public static void main(String[] args) {
  String amountString = JOptionPane.showInputDialog("Enter the amount you pay in cents,\n" +
        "followed by the cost of the item in cents.\n" +
        "The input must be in the form: pay:cost,\n" +
        "where pay is a whole number (100, 200, 300, etc) to a maximum of 900,\n" +
        "and cost is  a multiple of five (5, 10, 15, etc) to a maximum of 900");

  int pay, cost, change, originalChange, quarters, dimes, nickles;

  //this is the real meat of the solution
  String[] input = amountString.split(":");  //this creates a String array with 2 parts {"pay", "cost"}
  pay = Integer.parseInt(input[0]);  //This method (and the next) parse as int.
  cost = Integer.parseInt(input[1]);
  change = pay - cost;
  originalChange = change;

  quarters = change / 25;
  change = change % 25;
  dimes = change / 10;
  change = change %10;
  nickles = change / 5;

  JOptionPane.showMessageDialog(null, originalChange +
        " cents in coins can be given as:\n" +
        quarters + " quarters\n" +
        dimes + " dimes\n" +
        nickles + " nickles\n");
  System.exit(0);   //Note, unlike C++ This is unnecessary.
 //When the method ends there is no need to call System.exit(0);
 //The only time to use System.exit(0) is when it's outside of control flow.
 //The system was already going to end from the main method anyway so it doesn't change anything.
 //Pro tip: using return; (with nothing there) will also do the same thing.
}

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    如果您想将pay:costpaycostsubstring 分开,您必须首先知道: 的位置。因此,要获得: 所在的位置,您可以在String 中使用indexOf() 函数,在我的示例中为myString

    String myString = "pay:cost";
    
    int position = myString.indexOf("%");
    

    现在您可以substring 和您所拥有的String。我将创建两个变量来存储生成的两个部分。

    String pay = "";
    String cost = "";
    
    pay = myString.substring(0,position - 1);
    cost = myString.substring(position + 1, myString.length());
    

    注意:如果您想制作相同但更清晰的内容,可以使用split 函数。它创建了一个Strings 数组,以: 为引用,如下所示:

    String[] strings = myString.split(":");
    pay = strings[0]; --> "pay"
    cost = strings[1]; --> "cost"
    

    之后,如果您想将这些 Strings 转换为 ints,您只需:

    int payInt = Integer.parseInt(pay);
    int costInt = Integer.parseInt(cost);
    

    如果您对我上面的代码有疑问,请告诉我。

    希望对你有所帮助!

    【讨论】:

      【解决方案3】:

      拆分 pay:cost 的第一步是使用 string.split() 方法。

      http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

      if-else 语句的正确语法是

      if (pay > minimum_range || pay < maximum_range){
          do things
      
      else{
          do other things 
      }
      

      上述语句为“如果薪酬大于最小范围或小于最大范围,则执行操作。

      要检查它们是否具有以下语法,您需要执行以下操作。

      检查输入字符串中唯一的“字符”是否为 0-9 和冒号。你可以使用正则表达式来做到这一点,但你可能太新了,所以我建议使用一个简单的 for 循环来遍历字符串。

      for(int i = 0; i < str.length(); i++){
          if str.charAt(i) != Digit or Colon
              print error message
      }
      

      之后,您可以付费并找到合适的“零钱”。然后你可以从最大的数量(四分之一)开始循环,并不断减去 X 个季度,直到你不能再做为止。然后减去硬币,然后是镍,然后是便士,直到你完成。

      祝你好运!

      【讨论】:

      • 我尝试使用该代码来检查除 0-9 和冒号以外的字符。但是 Java 没有将“数字”或“冒号”识别为可接受的值。同样对于“i”,我应该将其替换为“支付”还是“成本”?
      猜你喜欢
      • 2018-03-09
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 2019-02-15
      相关资源
      最近更新 更多