当前位置: 首页 > 知识库问答 >
问题:

索引525附近的未闭合字符类[闭合]

方通
2023-03-14

编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答问题。

一般来说,我是一名新程序员,如果这个问题有一定的基础,请原谅我。

该程序的目标是简单地计算“最佳体重”,并在运行时在第35行的a和b字符串比较中不断抛出异常。我试图删除逻辑运算符,但这似乎仍然不是问题所在。我错在哪里?

import java.util.*;

public class WeightCalc {

  //Initialize variables
  static int feet = 0, inches = 0, totalWeight = 0;
  static boolean isMale;

  public static void optimalWeight(){
    // Calculate optimal weight
    if (isMale == true){
      if (feet >= 5){
        totalWeight = 106 + 6*(inches);
      } else{
        System.out.print("Error, you're a midget.");
      }
    }
    if (isMale == false){  
      if (feet >= 5){
        totalWeight = 100 + 5*(inches);
      } else{
        System.out.print("Error, you're a midget.");
      }
    }
  }

  public static void main(String[] args){

    String a = "Male", b = "male";

    // Initialize kboard Scanner
    Scanner kboard = new Scanner(System.in);

    // Ask for gender and assign isMale
    System.out.println("What is your gender? ");
    String gender = kboard.nextLine();
    if (gender.equals(a) || gender.equals(b)){
      isMale = true;
    }else {
      isMale = false;
    }

    // Ask for height, first feet, then inches
    System.out.println("What is your height in regards to feet? ");
    kboard.nextInt(feet);
    System.out.println("What is your remaining h eight in inches? ");
    kboard.nextInt(inches);

    //Call optimalWeight method and run
    optimalWeight();

    // Print the output
    System.out.println("Your optimal weight should be " + totalWeight + ".");

    // Set isMale opposite to what it was before and calculate opposite sex's potential weight
    isMale = !isMale;
    optimalWeight();

    // Print the output of the second run
    System.out.println("If you were of the opposite sex, your weight would be " + totalWeight + ".");

    // Close the Scanner variable
    kboard.close();
  }
}

共有2个答案

汪栋
2023-03-14

您调用了错误的扫描仪。readInt。您想调用返回int的空参数。

您调用的一个函数以int作为输入,该输入应该是数字基(即数字是八进制或二进制等)。由于英尺和英寸都初始化为0,因此它会尝试读取0基中的int,这是不可能的。

你想要的更像是:

// Ask for height, first feet, then inches 
System.out.println("What is your height in regards to feet? ");
feet = kboard.nextInt();
System.out.println("What is your remaining h eight in inches? ");
inches = kboard.nextInt();
通骁
2023-03-14

该行:

kboard.nextInt(feet);

应该是

feet = kboard.nextInt();

当您向Scanner.nextInt提供int参数时,它被认为是您要输入的数字的基数。英尺的值为零,您不能真正拥有基数为零的数字,因此出现错误。

请注意,您声明的行上不会出现错误:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 525
(([-+]?(((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&&[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))))|(((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&&[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))|(\Q-\E((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&&[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ^
    at java.util.regex.Pattern.error(Pattern.java:1955)
    at java.util.regex.Pattern.clazz(Pattern.java:2548)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.sequence(Pattern.java:2063)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.compile(Pattern.java:1696)
    at java.util.regex.Pattern.<init>(Pattern.java:1351)
    at java.util.regex.Pattern.compile(Pattern.java:1028)
    at java.util.Scanner$1.create(Scanner.java:367)
    at java.util.Scanner$1.create(Scanner.java:365)
    at sun.misc.LRUCache.forName(LRUCache.java:72)
    at java.util.Scanner.integerPattern(Scanner.java:443)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at WeightCalc.main(Main.java:45)

这里的倒数第二行:

java.util.Scanner.nextInt(Scanner.java:2117)

显示它在扫描仪中。下一步。

 类似资料: