2

我有这个简短的代码片段,我必须在其中检查一个字符串以查看它是否包含整数和可能的小数。该字符串是金额,(12.34)因此它也不能超过第四个索引。

我的问题是我被告知使用charAt()(并且在我的代码中我使用matches()并且contains()哪个是错误的)来检查整数和小数,以便如果字符串与这些参数一起使用,这个例程将返回一个布尔值,但是我'我对如何将其转换为 usecharAt()而不是matches()and感到困惑contains()

同样,如果我格式错误,或者措辞错误,或者代码看起来很糟糕,我很抱歉,我在 Java 的第一个学期,这是我上过的第一个编程课,所以我是有点粗糙。

import java.util.Scanner;

public class Auction
{
    public static void main(String[] args)
    {
      Scanner keyboard = new Scanner(System.in);
      String price;
      String quantity;

      System.out.print("How much money are you willing to bet on this item?: $");
      price = keyboard.next();


      if(price.matches("[0-9]*") && price.length() <= 5)
      {
         Float f = Float.parseFloat(price);
         System.out.printf("$%5.2f", f);
         System.out.println();
      }

      else if(price.matches("[0-9]*") && price.length() <= 5 && price.contains("."))
      {
         Float f = Float.parseFloat(price);
         System.out.printf("$%5.2f", f);
         System.out.println();
      }

      else
      {
         System.out.println("Invalid input");
      }

      System.out.print("What quantity do you want to bid on?: ");
      quantity = keyboard.next();
      if(quantity.contains("."))
      {
         System.out.println("Invalid input");
      }
    }
}
4

2 回答 2

1

我正在用手机输入这个。所以请原谅错误。您的教授是否要求您使用 charAt 而不是正则表达式和匹配项?

if (inpString!= null && !inpString.isEmpty () && inpString.length() <= 5){
   int periodCount = 0;
   for (int i=0; i  < inpString.length (); i++){  
      char c = inpString.charAt (i);
      if (c == '.'){
         periodCount++;
      }else if (c >= '0' && c  <= '9'){


      }else {
              System.out.println("invalid output");
              break;
      }
      if(periodCount > 1){
          System.out.println("too may periods. Invalid output"); 
          break;
      }


   }
}else { 
       System.out.println ("invalid input");
    }

如果您需要检查是否没有千位数字(即 1.234),您能否发表评论?如果是,请确保

          inpString.substring       
              (inpString.lastIndexOf 
                 (".")).length < 3 

使用所有 null 和 indexOutOfBounds 检查

于 2015-11-15T05:40:58.237 回答
0

这条路怎么样?

导入 java.util.Scanner;

公共类拍卖{

private static final String numberRegex = "^\\d*(\\.\\d+)?$";
private static final String integerNumber = "^\\d*$";
private static final int MAX_LENGTH = 5;

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    String price;
    String quantity;

    System.out.print("How much money are you willing to bet on this item?: $");
    price = keyboard.next();

    if (price.length() <= MAX_LENGTH && price.matches(numberRegex)) {
        Float f = Float.parseFloat(price);
        System.out.printf("$%5.2f\n", f);
    } else {
        System.out.println("Invalid input");
        return;
    }

    System.out.print("What quantity do you want to bid on?: ");

    quantity = keyboard.next();
    if (!quantity.matches(integerNumber)) {
        System.out.println("Invalid input");
    }
}

}

于 2015-11-15T05:07:11.377 回答