java.util.Scanner#nextFloat ( )源码实例Demo

下面列出了java.util.Scanner#nextFloat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: JavaCommon   文件: ScannerDemo.java
public static void scannerDemo1() {
	Scanner scan = new Scanner(System.in);
	// 从键盘接收数据
	int i = 0;
	float f = 0.0f;
	System.out.print("输入整数:");
	if (scan.hasNextInt()) {
		// 判断输入的是否是整数
		i = scan.nextInt();
		// 接收整数
		System.out.println("整数数据:" + i);
	} else {
		// 输入错误的信息
		System.out.println("输入的不是整数!");
	}
	System.out.print("输入小数:");
	if (scan.hasNextFloat()) {
		// 判断输入的是否是小数
		f = scan.nextFloat();
		// 接收小数
		System.out.println("小数数据:" + f);
	} else {
		// 输入错误的信息
		System.out.println("输入的不是小数!");
	}
}
 
源代码2 项目: java-tutorial   文件: ShowGrade.java
/**
 * 根据成绩计算成绩等级.
 *
 * @param args 命令行参数
 */
public static void main(String[] args) { // |\longremark{自动生成main函数的小技巧:输入psvm然后按下tab键即可}|
	Scanner console = new Scanner(System.in);
	System.out.print("请输入成绩:"); // |\longremark{在光标闪烁处输入成绩}|
	float grade = console.nextFloat();
	System.out.print("成绩" + grade + "的等级为:");
	if (grade >= 90) {
		System.out.println("优秀");
	} else if (grade >= 80) {
		System.out.println("良好");
	} else if (grade >= 70) {
		System.out.println("中等");
	} else if (grade >= 60) {
		System.out.println("及格");
	} else {
		System.out.println("不及格");
	}
}
 
源代码3 项目: java-tutorial   文件: ShowGrade.java
public static void main(String[] args) {
  String gradeLevel = "";
  Scanner console = new Scanner(System.in);
  System.out.print("请输入成绩:");
  float grade = console.nextFloat();
  int level = (int)grade / 10; // 转换为整数
  switch(level) {
    case 10:
    case 9:
      gradeLevel = "优秀";
      break;
    case 8:
      gradeLevel = "良好";
      break;
    case 7:
      gradeLevel = "中等";
      break;
    case 6:
      gradeLevel = "及格";
      break;
    default:
      gradeLevel = "不及格";
  }

  System.out.println("输入成绩为:" + grade + ",成绩等级为:" + gradeLevel);
}
 
源代码4 项目: Project   文件: Main22.java
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

 //   while (scanner.hasNextLine()){
        float value = scanner.nextFloat();
        String inputString = String.valueOf(value);
        fun(inputString);
   // }

    
    for (String s : resList) {
        System.out.println(s);
    }
}
 
源代码5 项目: Project   文件: Huawei6.java
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    float inputValue = scanner.nextFloat();
    String inputString = String.valueOf(inputValue);
    String[] splitValue = inputString.split("\\.");
    if (Integer.valueOf(splitValue[1]) >= 5) {
        System.out.println(Integer.valueOf(splitValue[0]) + 1);
    } else {
        System.out.println(Integer.valueOf(splitValue[0]));
    }
}
 
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Please input a float number: ");
    float a = in.nextFloat(); //接收float数据
    System.out.println("Please input a string:"); //中间有空格的string就只能显示第一个空格前的内容
    Scanner str = new Scanner(System.in);
    System.out.println("The string is: " + str.next());
    System.out.println("The float number is: " + a);
    for (int i = 0; i < 4; i++) {
        System.out.println("Please input a int number:");
        int b = in.nextInt();
        System.out.println("The int number is: " + b);
    }
}
 
源代码7 项目: db   文件: DataCubeColumn.java
/**
 *
 * @param val
 * @return
 * @throws IllegalArgumentException
 */
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);
    return sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
源代码8 项目: db   文件: DataCubeDimension.java
/**
 *
 * @param val
 * @return
 * @throws IllegalArgumentException
 */
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);
    return sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
源代码9 项目: db   文件: SchemaBuilder.java
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);

    return sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
源代码10 项目: javacore   文件: ScannerDemo.java
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in); // 从键盘接收数据
    int i = 0;
    float f = 0.0f;
    System.out.print("输入整数:");
    if (scan.hasNextInt()) { // 判断输入的是否是整数
        i = scan.nextInt(); // 接收整数
        System.out.println("整数数据:" + i);
    } else {
        System.out.println("输入的不是整数!");
    }

    System.out.print("输入小数:");
    if (scan.hasNextFloat()) { // 判断输入的是否是小数
        f = scan.nextFloat(); // 接收小数
        System.out.println("小数数据:" + f);
    } else {
        System.out.println("输入的不是小数!");
    }

    Date date = null;
    String str = null;
    System.out.print("输入日期(yyyy-MM-dd):");
    if (scan.hasNext("^\\d{4}-\\d{2}-\\d{2}$")) { // 判断
        str = scan.next("^\\d{4}-\\d{2}-\\d{2}$"); // 接收
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(str);
        } catch (Exception e) {
        }
    } else {
        System.out.println("输入的日期格式错误!");
    }
    System.out.println(date);
}
 
源代码11 项目: java-tutorial   文件: CircleArea.java
/**
 * main函数是Java程序的执行入口,这一点和C语言很相似.
 *
 * @param args 命令行参数,和C语言的argv也很相似
 */
public static void main(String[] args) {
	Scanner console = new Scanner(System.in);
	System.out.print("请输入半径:");// |\longremark{print函数只输出内容,不会自动换行}|
	float radius = console.nextFloat();
	double area = PAI * radius * radius; // |\longremark{表达式的最终类型是double,和C语言的类型转换规则是相同的}|
	System.out.println("面积=" + area); // |\longremark{println输出内容后自动换行。请思考如何使得输出结果只精确到小数点后3位? }|
}
 
源代码12 项目: java-tutorial   文件: ShowGradeMulti.java
public static void main(String[] args) {
  float grade = 0; //  百分制成绩
  Scanner console = new Scanner(System.in);
  System.out.print("请输入成绩:");
  grade = console.nextFloat();
  while(grade >= 0) {
    showGrade(grade);
    grade = console.nextFloat();
  }
}
 
源代码13 项目: JavaExercises   文件: LunarFlyOne.java
private void command() {
    Scanner sc = new Scanner(System.in);
    System.out.println(showConstants());
    init();
    while (!isLanding) {
        fuel = sc.nextInt();
        duration = sc.nextFloat();
        float reverse = Math.signum(fuel);
        fuel = Math.abs(fuel);
        simulate(reverse);
    }
}
 
源代码14 项目: JOpenShowVar   文件: Variable.java
/**
     * Parses a string encoded KRL variable to a JOpenShowVar Variable
     *
     * @param callback the Callback from the robot
     * @return the JOpenShowVar Variable
     * @throws NumberFormatException on parsing error
     */
    public static Variable parseVariable(Callback callback) throws NumberFormatException {
        String variable = callback.getVariableName();
        String value = callback.getStringValue();
        int id = callback.getId();
        long readTime = callback.getReadTime();
//        int option = callback.getOption();

        Scanner sc = new Scanner(value);
        Variable var;
        if (sc.hasNextInt()) {
            var = new Int(id, variable, sc.nextInt(), readTime);
            sc.close();
        } else if (sc.hasNextFloat()) {
            var = new Real(id, variable, (double) sc.nextFloat(), readTime);
            sc.close();
        } else if (sc.hasNextDouble()) {
            var = new Real(id, variable, sc.nextDouble(), readTime);
            sc.close();
        } else if (sc.hasNextBoolean()) {
            var = new Bool(id, variable, sc.nextBoolean(), readTime);
            sc.close();
        }else if (value.contains("#")) {
            var = new Enum(id, variable, sc.nextLine(), readTime);
            sc.close();
        } else if (value.contains("{")) {
            sc.close();
            var = new Struct(id, variable, Struct.parseString(value), readTime);
        } else {
            var = new Real(id, variable, Double.parseDouble(value), readTime);
            sc.close();
        }
        return var;
    }
 
源代码15 项目: localization_nifi   文件: AvroRecordConverter.java
/**
 * Converts the data from one schema to another. If the types are the same,
 * no change will be made, but simple conversions will be attempted for
 * other types.
 *
 * @param content
 *            The data to convert, generally taken from a field in an input
 *            Record.
 * @param inputSchema
 *            The schema of the content object
 * @param outputSchema
 *            The schema to convert to.
 * @return The content object, converted to the output schema.
 * @throws AvroConversionException
 *             When conversion is impossible, either because the output type
 *             is not supported or because numeric data failed to parse.
 */
private Object convertData(Object content, Schema inputSchema,
        Schema outputSchema) throws AvroConversionException {
    if (content == null) {
        // No conversion can happen here.
        if (supportsNull(outputSchema)) {
            return null;
        }
        throw new AvroConversionException("Output schema " + outputSchema
                + " does not support null");
    }

    Schema nonNillInput = getNonNullSchema(inputSchema);
    Schema nonNillOutput = getNonNullSchema(outputSchema);
    if (nonNillInput.getType().equals(nonNillOutput.getType())) {
        return content;
    } else {
        if (nonNillOutput.getType() == Schema.Type.STRING) {
            return content.toString();
        }

        // For the non-string cases of these, we will try to convert through
        // string using Scanner to validate types. This means we could
        // return questionable results when a String starts with a number
        // but then contains other content
        Scanner scanner = new Scanner(content.toString());
        scanner.useLocale(locale);
        switch (nonNillOutput.getType()) {
        case LONG:
            if (scanner.hasNextLong()) {
                return scanner.nextLong();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to long");
            }
        case INT:
            if (scanner.hasNextInt()) {
                return scanner.nextInt();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to int");
            }
        case DOUBLE:
            if (scanner.hasNextDouble()) {
                return scanner.nextDouble();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to double");
            }
        case FLOAT:
            if (scanner.hasNextFloat()) {
                return scanner.nextFloat();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to float");
            }
        default:
            throw new AvroConversionException("Cannot convert to type "
                    + nonNillOutput.getType());
        }
    }
}
 
源代码16 项目: java-tutorial   文件: Ball.java
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.println("请输入球体的半径:");
    float radius = console.nextFloat();
    System.out.println("球体的体积为=" + volume(radius));
}
 
源代码17 项目: Java   文件: PerlinNoise.java
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    final int width;
    final int height;
    final int octaveCount;
    final float persistence;
    final long seed;
    final String charset;
    final float[][] perlinNoise;

    System.out.println("Width (int): ");
    width = in.nextInt();

    System.out.println("Height (int): ");
    height = in.nextInt();

    System.out.println("Octave count (int): ");
    octaveCount = in.nextInt();

    System.out.println("Persistence (float): ");
    persistence = in.nextFloat();

    System.out.println("Seed (long): ");
    seed = in.nextLong();

    System.out.println("Charset (String): ");
    charset = in.next();


    perlinNoise = generatePerlinNoise(width, height, octaveCount, persistence, seed);
    final char[] chars = charset.toCharArray();
    final int length = chars.length;
    final float step = 1f / length;
    //output based on charset
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            float value = step;
            float noiseValue = perlinNoise[x][y];

            for (char c : chars) {
                if (noiseValue <= value) {
                    System.out.print(c);
                    break;
                }

                value += step;
            }
        }

        System.out.println();
    }
    in.close();
}
 
源代码18 项目: nifi   文件: AvroRecordConverter.java
/**
 * Converts the data from one schema to another. If the types are the same,
 * no change will be made, but simple conversions will be attempted for
 * other types.
 *
 * @param content
 *            The data to convert, generally taken from a field in an input
 *            Record.
 * @param inputSchema
 *            The schema of the content object
 * @param outputSchema
 *            The schema to convert to.
 * @return The content object, converted to the output schema.
 * @throws AvroConversionException
 *             When conversion is impossible, either because the output type
 *             is not supported or because numeric data failed to parse.
 */
private Object convertData(Object content, Schema inputSchema,
        Schema outputSchema) throws AvroConversionException {
    if (content == null) {
        // No conversion can happen here.
        if (supportsNull(outputSchema)) {
            return null;
        }
        throw new AvroConversionException("Output schema " + outputSchema
                + " does not support null");
    }

    Schema nonNillInput = getNonNullSchema(inputSchema);
    Schema nonNillOutput = getNonNullSchema(outputSchema);
    if (nonNillInput.getType().equals(nonNillOutput.getType())) {
        return content;
    } else {
        if (nonNillOutput.getType() == Schema.Type.STRING) {
            return content.toString();
        }

        // For the non-string cases of these, we will try to convert through
        // string using Scanner to validate types. This means we could
        // return questionable results when a String starts with a number
        // but then contains other content
        Scanner scanner = new Scanner(content.toString());
        scanner.useLocale(locale);
        switch (nonNillOutput.getType()) {
        case LONG:
            if (scanner.hasNextLong()) {
                return scanner.nextLong();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to long");
            }
        case INT:
            if (scanner.hasNextInt()) {
                return scanner.nextInt();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to int");
            }
        case DOUBLE:
            if (scanner.hasNextDouble()) {
                return scanner.nextDouble();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to double");
            }
        case FLOAT:
            if (scanner.hasNextFloat()) {
                return scanner.nextFloat();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to float");
            }
        default:
            throw new AvroConversionException("Cannot convert to type "
                    + nonNillOutput.getType());
        }
    }
}