我是一名新的程序员,如果这个问题有点基础,请原谅我。
该程序的目标是对“最佳体重”进行简单计算,并在运行期间通过第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();
}
}
答案 0 :(得分:1)
这一行:
kboard.nextInt(feet);
应该是
feet = kboard.nextInt();
当您向Scanner.nextInt
提供int参数时,它被视为您要输入的数字的基数。 feet
的值为零,您实际上没有基数零数,因此错误。
请注意错误doesn't occur on the line you claim:
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
。
答案 1 :(得分:0)
您正在拨打错误的Scanner.readInt
。您想要调用返回int
的空args。
你调用的那个接受int
作为输入,它应该是数字基数(即数字是八进制或二进制等)。由于feet
和inches
都被初始化为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();