Java Code Examples for java.util.Scanner#nextFloat()

The following examples show how to use java.util.Scanner#nextFloat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShowGrade.java    From java-tutorial with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: ShowGrade.java    From java-tutorial with Apache License 2.0 6 votes vote down vote up
/**
 * 根据成绩计算成绩等级.
 *
 * @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("不及格");
	}
}
 
Example 3
Source File: ScannerDemo.java    From JavaCommon with Apache License 2.0 6 votes vote down vote up
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("输入的不是小数!");
	}
}
 
Example 4
Source File: Huawei6.java    From Project with Apache License 2.0 5 votes vote down vote up
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]));
    }
}
 
Example 5
Source File: Variable.java    From JOpenShowVar with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
     * 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;
    }
 
Example 6
Source File: LunarFlyOne.java    From JavaExercises with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 7
Source File: ShowGradeMulti.java    From java-tutorial with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 8
Source File: CircleArea.java    From java-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 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位? }|
}
 
Example 9
Source File: ScannerDemo.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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);
}
 
Example 10
Source File: SchemaBuilder.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: DataCubeDimension.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @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;
}
 
Example 12
Source File: DataCubeColumn.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @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;
}
 
Example 13
Source File: InputTest.java    From Algorithms-Fourth-Edition-Exercises with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 14
Source File: Main22.java    From Project with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 15
Source File: Ball.java    From java-tutorial with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.println("请输入球体的半径:");
    float radius = console.nextFloat();
    System.out.println("球体的体积为=" + volume(radius));
}
 
Example 16
Source File: AvroRecordConverter.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * 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());
        }
    }
}
 
Example 17
Source File: PerlinNoise.java    From Java with MIT License 4 votes vote down vote up
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();
}
 
Example 18
Source File: AvroRecordConverter.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * 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());
        }
    }
}