Java 索引525附近的未闭合字符类
我是一个新的程序员,所以请原谅我,如果这个问题是一个公平的基础 该程序的目标是简单地计算“最佳体重”,并在运行时在第35行的a和b字符串比较中持续抛出异常。我已尝试删除逻辑运算符,但这似乎仍然不是问题所在。我错在哪里Java 索引525附近的未闭合字符类,java,regex,patternsyntaxexception,Java,Regex,Patternsyntaxexception,我是一个新的程序员,所以请原谅我,如果这个问题是一个公平的基础 该程序的目标是简单地计算“最佳体重”,并在运行时在第35行的a和b字符串比较中持续抛出异常。我已尝试删除逻辑运算符,但这似乎仍然不是问题所在。我错在哪里 import java.util.*; public class WeightCalc { //Initialize variables static int feet = 0, inches = 0, totalWeight = 0; static boolean
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();
}
}
这一行:
kboard.nextInt(feet);
应该是
feet = kboard.nextInt();
当您向Scanner.nextInt提供int参数时,它被认为是要输入的数字的基数Foots
的值为零,您不能真正拥有基数为零的数字,因此会出现错误
请注意,错误:
显示它在
Scanner.nextInt
中,您调用的Scanner.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();
第35行是哪一行
if(gender.equals(a)| gender.equals(b)){
?有什么例外?不要使用a==true
或a==false
来比较布尔值。只需使用a
或!a
。再次检查行号,因为在if(gender.equals(a)| gender.equals(b)行中没有正则表达式处理
。更可能的是,它在kboard.nextInt(feet)
行中,应该是feet=kboard.nextInt()
。我认为这实际上是OpenJDK中的一个错误-与其说是行为,不如说是隐藏的错误。该方法似乎可以检查基数<2
。但是,这可能不是常见的问题。@AndyTurnerInteger.parseInt(字符串s,int基数)
定义它将对一个坏的基数值抛出NumberFormatException
,而nextInt(int-radix)
定义它将调用parseInt()
,因此他们可能依赖于它的验证,在调用parseInt()之前没有意识到正则表达式对0
基数是坏的
。重要的一行不是WeightCalc.main(main.java:45)
,这表明是第45行造成的吗?@andrew,当然,但是您可以看到,调用nextInt
时没有查看行号,这是一个问题。
java.util.Scanner.nextInt(Scanner.java:2117)
// 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();