如何创建一个使用Scanner类来接收用户输入并进行注册的方法?

huangapple go评论137阅读模式
英文:

How can I create a method that uses the Scanner class to register the user's input?

问题

public static double leeTeclado() {
Scanner radio = new Scanner(System.in); // Create the scanner object
System.out.println("Enter the value of the radius: "); // Ask the user to input a value
double radiusValue = radio.nextDouble(); // The Scanner reads the value

try {
    radiusValue = Double.parseDouble(radiusValue);
}
catch (Exception e) {
    System.out.println("Error in data reading");
}
return radiusValue;

}

英文:

I need the user to input a radius value, so that, I can calculate the area/perimeter of a circle. Since I can't really understand the Scanner class, I can't seem to make it work. I would love some feedback to fix my method.

public static double leeTeclado() {
    Scanner radio = new Scanner(System.in); // Creamos el objeto de scanner
    System.out.println("Escribe el valor del radio: "); // Pedimos al usuario que introduzca un valor
    radio = radio.nextDouble();       // El Scanner lee el valor


    try {
        radio = Double.parseDouble(radio.nextDouble());
    }
    catch (Exception e) {
        System.out.println("Error en la lectura de datos");
    }
    return radio;

}

答案1

得分: 0

这一行有问题,因为你将一个 double 赋值给了 Scanner 类的实例:radio = radio.nextDouble();

尝试定义另一个类型为 double 的变量,将 radio.nextDouble() 的结果赋值给它:

public static double leeTeclado() {
    Scanner radio = new Scanner(System.in); // 创建 Scanner 对象
    System.out.println("请输入半径的值:"); // 提示用户输入值
    double= 0.0;
    try {
= radio.nextDouble();       // Scanner 读取值
    } catch (Exception e) {
      e.printStackTrace();
    }
    return;
}
英文:

This line is a problem because you assign a double to an instance of the Scanner class: radio = radio.nextDouble();

Try to define another variable of type double and assign the result of radio.nextDouble() to it:

public static double leeTeclado() {
    Scanner radio = new Scanner(System.in); // Creamos el objeto de scanner
    System.out.println("Escribe el valor del radio: "); // Pedimos al usuario que introduzca un valor
    double valor = 0.0;
    try {
       valor = radio.nextDouble();       // El Scanner lee el valor
    } catch (Exception e) {
      e.printStackTrace();
    }
    return valor;
}

答案2

得分: 0

// 文件名: Demo.java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入圆的半径: ");
        double radius = sc.nextDouble();

        double diameter = (radius * 2);
        double perimeter = ((22 * diameter) / 7);
        double area = ((22 * radius * radius) / 7);
        System.out.println("半径 = " + radius);
        System.out.println("直径 = " + diameter);
        System.out.println("周长 = " + perimeter);
        System.out.println("面积 = " + area);
    }
}

输出:

> javac Demo.java

> java Demo

输入圆的半径: 4
半径 = 4.0
直径 = 8.0
周长 = 25.142857142857142
面积 = 50.285714285714285
英文:

The nextFloat() or nextDouble() method of java.util.Scanner can be used for implementing the desired requirement of reading floatig point input.

Working demo of nextFloat() / nextDouble():

// File name: Demo.java
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter the radius of circle: ");
		double radius = sc.nextDouble();

		double diameter = (radius * 2);
		double perimeter = ((22 * diameter) / 7);
		double area = ( (22 * radius * radius) / 7);
		System.out.println("Radius = " + radius);
		System.out.println("Diameter = " + diameter);
		System.out.println("Perimeter = " + perimeter);
		System.out.println("Area = " + area);
	}
}

Output:

> javac Demo.java

> java Demo

Enter the radius of circle: 4
Radius = 4.0
Diameter = 8.0
Perimeter = 25.142857142857142
Area = 50.285714285714285

答案3

得分: 0

<br/>
你做错的是将一个双精度值赋给了 Scanner 的一个实例。<br/>
这样实际上行不通。<br/>
一个正确代码的示例是

```java
static double getRadius () {
    Scanner scan = new Scanner (System.in);

    System.out.println("输入半径值:");

    // 你将会把半径的值存储在这个变量中
    double radius;

    // 为了捕获可能的数字格式异常而设置的 try-catch 块
    try {
        radius = scan.nextDouble();
    } catch (Exception exc) {
        exc.printStackTrace();
    }

    // 如果没有发生异常,返回这个值
    return radius;
}

要更好地理解 Scanner 类,请查看这些链接:https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
https://www.w3schools.com/java/java_user_input.asp

希望能对你有所帮助


<details>
<summary>英文:</summary>

&lt;br/&gt;
what you&#39;re doing wrong is assigning a double value to an instance of Scanner. &lt;br/&gt;
that doesn&#39;t really work. &lt;br/&gt;
an example of correct code would be 

```java
static double getRadius () {
    Scanner scan = new Scanner (System.in);

    System.out.println(&quot;Enter radius value : &quot;);

    // you will store the value of the radius in this variable 
    double radius;

    // try-catch block for eventual number format exceptions 
    try {
        radius = scan.nextDouble();
    } catch (Exception exc) {
        exc.printStackTrace();
    }

    // if no exception occured, return the value
    return radius;
}

to better understand the Scanner class, take a look at these links : https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
https://www.w3schools.com/java/java_user_input.asp

hope I helped you

huangapple
  • 本文由 发表于 2020年9月7日 04:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63768602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定