本文整理汇总了Java中java.util.Scanner.nextFloat方法的典型用法代码示例。如果您正苦于以下问题:Java Scanner.nextFloat方法的具体用法?Java Scanner.nextFloat怎么用?Java Scanner.nextFloat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Scanner
的用法示例。
在下文中一共展示了Scanner.nextFloat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: scannerDemo1
import java.util.Scanner; //导入方法依赖的package包/类
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: main
import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float[] salary = new float[10];
for (int i = 0; i < salary.length; i++) {
System.out.printf("Salário do %dº funcionário: R$ ", (i+1));
salary[i] = input.nextFloat();
}
input.close();
for (int i = 0; i < salary.length; i++) {
if (salary[i] < 860.0F)
salary[i] *= 1.10F;
else if ((salary[i] > 860.0F) && (salary[i] < 1200.0F))
salary[i] *= 1.07F;
else
salary[i] *= 1.05F;
}
for (int i = 0; i < salary.length; i++) {
System.out.printf("\nSalário do %dº funcionário (reajuste): R$ %.2f", (i+1), salary[i]);
}
System.out.println("");
}
示例3: main
import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
// entrada = preco de custo + percentual
// Seria o prompt do java script
Scanner sc = new Scanner(System.in);
System.out.println("Digite o preco custo:");
double precoCusto = sc.nextDouble();
System.out.println("Digite o percentual:");
float percentual = sc.nextFloat();
// processamento = valor de venda
double valorVenda = (percentual / 100 * precoCusto) + precoCusto;
// saida = valor da venda
// Forma de arredondar um valor
valorVenda = Math.round(valorVenda);
System.out.println("Valor de venda = " + valorVenda);
}
示例4: CalcBisectionRoot
import java.util.Scanner; //导入方法依赖的package包/类
void CalcBisectionRoot() /*Function definition to calculating the root by the method of Bisection*/
{
double interval1 = 0.0,interval2 = 0.0,root=0.0,k = 0;
Scanner s=new Scanner(System.in);
System.out.println("Enter The Value Of interval1 :"); /*Taking the value from first interval of the function from user*/
interval1=s.nextFloat();
System.out.println("Enter The Value Of interval2 :"); /*Taking the value from second interval of the function from user*/
interval2=s.nextFloat();
if((f(interval1)*f(interval2))<0) /*Multiplication of the function of two interverals where the root lies between, must be less than zero*/
{
while(Math.abs(interval1-interval2)>EPSILON)
{
root=(interval1+interval2)/2; /*iterative formula of Bisection method*/
k=f(root);
if(f(interval1)*k>0)
interval1=root;
else
interval2=root;
}
System.out.println("The Calculated Root Is::"+root); /*printing the desired root*/
}
else
System.out.println("No real roots on this interval. OPERATION ABORTED."); /*print when root doesn't found*/
}
示例5: main
import java.util.Scanner; //导入方法依赖的package包/类
/**
* 根据成绩计算成绩等级.
*
* @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("不及格");
}
}
示例6: main
import java.util.Scanner; //导入方法依赖的package包/类
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);
}
示例7: main
import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
//el nombre del scanner puede ser cualquiera
Scanner pepe = new Scanner(System.in);
//int se utiliza para enteros
System.out.println("Ingrese un entero");
int a = pepe.nextInt();
//float se utiliza para numeros con coma
System.out.println("Ingrese un numero con coma");
float b = pepe.nextFloat();
//String se utiliza para palabras
System.out.println("Ingrese un string (palabra)");
String s = pepe.nextLine();
System.out.println("Vemos el contenido");
System.out.println(a);
System.out.println(b);
System.out.println(s);
}
示例8: main
import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); // initialize the input scanner
Distinct foo = new Distinct(); // initialize the object instance
System.out.println("Enter the number of values in the array: ");
int nLen = scan.nextInt();
System.out.println("Enter the array contents: ");
float[] array = new float[nLen];
for (int i = 0; i < nLen; i++) {
array[i] = scan.nextFloat();
}
boolean bDist = foo.isDistinct(array);
if (bDist) {
System.out.println("The numbers in the array are distinct");
}
else {
System.out.println("There are at least two numbers equal");
}
}
示例9: main
import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
Session ses = null; //creating the session object
Emp em = null;
Transaction tx = null; //creating transaction object
Scanner sc=new Scanner(System.in); //creating scanner for user input
ses = HibernateUtil.getSession(); //get the cgf,factory,session
em = new Emp(); //creating the domain class object
System.out.print("\n\t\tEnter the Account Holder Name: ");
String name=sc.nextLine();
System.out.print("\n\t\tEnter the Account Type: ");
String type=sc.nextLine();
System.out.print("\n\t\tEnter the Account Holder Balance: ");
float bal=sc.nextFloat();
em.setHoldername(name);
em.setAcctype(type);
em.setBalance(bal);
try {
tx = ses.beginTransaction();
ses.save(em);
tx.commit();
System.out.println("object is saved with account number: "+em.getAccno());
//System.out.print("\n\t\tEnter the Account Holder Name: ");
} catch (Exception e) {
tx.rollback();
}
HibernateUtil.closeSession(ses);
HibernateUtil.closeFactory();
System.out.print("\n\t\t ==PROGRAMME BY PRATIK DIMBLE==\n\n\n");
}
示例10: main
import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int TAM = 10;
String name[] = new String[TAM];
float bi1[] = new float[TAM];
float bi2[] = new float[TAM];
float average[] = new float[TAM];
for (int i = 0; i < TAM; i++) {
System.out.printf("Nome do %dº aluno: ", (i+1));
name[i] = input.next();
System.out.print("Nota do 1º bimestre: ");
bi1[i] = input.nextFloat();
System.out.print("Nota do 2º bimestre: ");
bi2[i] = input.nextFloat();
System.out.println("");
}
input.close();
for (int i = 0; i < TAM; i++) {
average[i] = bi1[i] * 0.4F + bi2[i] * 0.6F;
}
for (int i = 0; i < TAM; i++) {
System.out.printf("%s: %s\n", name[i],
average[i] >= 6.0 ? "APROVADO" : "REPROVADO");
}
}
示例11: main
import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float array[] = new float[10];
float average = 0.0F;
for (int i = 0; i < array.length; i++) {
System.out.printf("%dº número real: ", (i+1));
array[i] = input.nextFloat();
average += array[i];
}
input.close();
average /= array.length;
System.out.printf("\nMédia Simples: %.1f\n", average);
}
示例12: main
import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int TAM = 10;
float array1[] = new float[TAM];
float array2[] = new float[TAM];
float array3[] = new float[TAM];
System.out.println("1º vetor");
for (int i = 0; i < TAM; i++) {
System.out.printf("%dº valor: ", (i+1));
array1[i] = input.nextFloat();
}
System.out.println("2º vetor");
for (int i = 0; i < TAM; i++) {
System.out.printf("%dº valor: ", (i+1));
array2[i] = input.nextFloat();
}
input.close();
for (int i = 0; i < TAM; i++) {
array3[i] = array1[i] * array2[i];
}
for (int i = 0; i < TAM; i++) {
System.out.printf("\narray3[%d] = %.2f\n", i, array3[i]);
}
}
示例13: soupStoreUserIn
import java.util.Scanner; //导入方法依赖的package包/类
/**
* Stores the user input
* @param i index to be passed to parser
* @param cache line of code from main loop
* @throws NumberFormatException
* @throws SoupVariableException
* @throws SoupSyntaxException
*/
public static void soupStoreUserIn(int i, String cache) throws NumberFormatException, SoupVariableException, SoupSyntaxException {
ns = Parser.parse(i, cache);
HTMLGen.questionStrings.add(ns.get(0));
List<String> validation = new ArrayList<String>();
for (int e = 1; e < ns.size(); e++) {
validation.add(ns.get(e));
}
Validator.validateNumbers(validation);
if (!lockIndex)
index = Parser.inx;
System.out.println(ns.get(0));
Scanner s = new Scanner(System.in);
if (s.hasNextFloat()) {
lastResult = s.nextFloat();
VariableHandler.insertVar(lastResult, Integer.parseInt(ns.get(1)));
}
else {
switch (s.nextLine()) {
case "`" :
VariableHandler.insertVar(1, 100);
break;
case "." :
VariableHandler.insertVar(1, 101);
break;
}
}
}
示例14: main
import java.util.Scanner; //导入方法依赖的package包/类
/**
* 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位? }|
}
示例15: main
import java.util.Scanner; //导入方法依赖的package包/类
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();
}
}