Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 索引525附近的未闭合字符类_Java_Regex_Patternsyntaxexception - Fatal编程技术网

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

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

我是一个新的程序员,所以请原谅我,如果这个问题是一个公平的基础

该程序的目标是简单地计算“最佳体重”,并在运行时在第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();
  }
}
这一行:

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
。但是,这可能不是常见的问题。@AndyTurner
Integer.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();