更改程序中的字符串、子字符串和 if-else 语句

String, Substring, and if-else Statements in Change- Making Program

我这周刚开始 Java class,我们的第一个项目将于周日到期,我在这方面遇到了一些麻烦。该项目的基本原理是我们必须创建一个程序来计算购买后返还的零钱,并告诉用户有多少 25 美分、10 美分和 5 分硬币构成了所说的零钱。

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

这是他对项目的逐字说明,以及我现在拥有的代码。请记住,这是宝宝的第一个项目,所以请手下留情。感谢任何帮助。

说明:

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

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

使用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.
}

希望对您有所帮助。

拆分 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 
}

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

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

检查输入字符串中仅有的 'chars' 是 0-9 和冒号。您可以使用正则表达式执行此操作,但您可能太新了,所以我建议使用简单的 for 循环来遍历字符串。

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

之后你可以支付 - 费用并找到合适的 'change'。然后您可以从最大数量(四分之一)开始循环并继续减去 X 个季度,直到您不能再减去为止。然后减去 10 美分,然后是 5 美分,然后是 1 美分,直到你完成。

祝你好运!

如果您想将 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);

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

希望对您有所帮助!