有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

更改程序中的java字符串、子字符串和ifelse语句

我这周刚开始上Java课,我们的第一个项目周日就要交了,我有点麻烦。该项目的基本jist是,我们必须创建一个程序来计算购买后返回的更改,并告诉用户有多少个25美分、10美分和5美分组成了所述更改

问题是,老师希望输入的形式是“pay:cost”,这不适合任何原始类型。所以他建议用一个字符串和子字符串将“pay”和“cost”与冒号分开,但我不知道该怎么做“支付”和“成本”也必须是特定范围内的数字,我认为这就是“如果其他声明”的含义。问题是我对语法没有很好的理解,需要一些帮助来设置它

以下是他为项目逐字给出的说明,以及我现在掌握的代码。请记住,这是宝宝的第一个项目,所以请慈悲。感谢您的帮助

说明:

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

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

  • 工资是一个整数(100,200,300,…)。

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

  • 报酬大于或等于成本。

  • 成本大于或等于5,只能用四分之一、一角和五分镍币(即5、10、15、…95、100、105、…890、895或900)表示

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

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

4.)只能导入javax。摆动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);
  }
}

共 (3) 个答案

  1. # 1 楼答案

    拆分薪酬的第一步:成本是使用字符串。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个四分之一,直到你不能再减去为止。然后减去一角硬币,再减去五分镍币,再减去五分镍币,直到完成为止

    祝你好运

  2. # 2 楼答案

    如果你想用substring来分隔pay:costpaycost,你必须首先知道:在哪里。所以,要获得:它所在的位置,可以在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. # 3 楼答案

    使用字符串。拆分(“:”)以支付和支付两个单独的项目。然后用整数转换成整数。parseInt(字符串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.
    }
    

    希望这有帮助