-1

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

该程序的目标是“最佳体重”的简单计算,并在运行时不断抛出异常,超过第 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();
  }
}
4

2 回答 2

2

这一行:

kboard.nextInt(feet);

应该

feet = kboard.nextInt();

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


请注意,错误不会发生在您声明的行上

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)

表明它在Scanner.nextInt.

于 2016-09-16T18:59:07.470 回答
0

你叫错了Scanner.readInt。您想调用一个返回int.

您调用的那个将 aint作为输入,该输入应该是数字基数(即数字是八进制或二进制等)。由于feetinches都被初始化为 0,它试图读取一个int以 0 为底的值,这是不可能的。

你想要的更像是:

// 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();
于 2016-09-16T19:04:17.070 回答