gpt4 book ai didi

java - 扫描仪java验证和多个实例

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:26 24 4
gpt4 key购买 nike

我是java新手,正在做作业。我必须向用户请求 3 个输入并进行验证。如果我只使用扫描仪的一个实例来完成此操作,我就会一团糟。

如果我使用三个实例并进行一些解决方法,我的代码就可以工作。只是我猜这不是最佳实践。我已经阅读了一些有关扫描仪的手册,但无法理解问题

谢谢

enter code here
Scanner input=new Scanner(System.in);
Scanner input2=new Scanner(System.in);

int input_integer=0;
double input_double=0.0;
String input_string="";
double value=0;

System.out.print("\n Please enter a number: ");

while(!input.hasNextInt()){
System.out.println("***** Error: the char inserted is not a number! *****");
String input_wrong=input.next();
System.out.print("\n Please enter a number: ");
}

input_integer=input.nextInt();

System.out.print("\n Please enter a double: ");
while(!input.hasNextDouble()){
System.out.println("***** Error: the char inserted is not a double! *****");
String input_wrong=input.next();
System.out.print("\n Please enter an double: ");
}
input_double=input.nextDouble();

System.out.print("\nPlease enter a string: ");
input_string=input.nextLine();

因此,我有两个创建 3 个扫描仪实例,并且还使用字符串将 while 循环中的错误输入分配给能够再次提示。有什么建议吗?我确信有更好的方法,但我会尝试理解..

谢谢!

最佳答案

我不太确定我理解您遇到的问题,但扫描仪有一些奇怪的行为,这些行为并不立即明显。例如,如果输入“1234bubble”然后按 Enter 键,则 nextInt() 将返回 1234,下一个 nextLine() 将显示“bubble”。对于这样的输入,这通常不是所需的行为,因为“1234bubble”不是整数,当用户按下 Enter 时应该会失败。

因此,我通常只使用函数 nextLine()。然后,我只需使用 Integer.parseInt(..) 等函数手动处理数据。这样,我可以保证以清晰明了的方式处理整行代码,这与创建令人困惑的代码的其他技术不同。

以下是我编写程序的方式:

import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class Main
{
static Random rand = new Random();

public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(System.in);

int input_integer = 0;
double input_double = 0.0;
String input_string = "";
double value = 0;

while (true)
{
System.out.print("Please enter an integer: ");

// Get the entire next line of text
String text = input.nextLine();

try
{
// Try to turn the line into an integer
input_integer = Integer.parseInt(text);

// Turning it into an int succeeded!
// Leave the while loop
break;
} catch (NumberFormatException e)
{
// Turning it into an int failed.
System.out.println("***** Error: the text inserted is not an integer! *****");
}
}

while (true)
{
System.out.print("Please enter a double: ");

// Get the entire next line of text
String text = input.nextLine();

try
{
// Try to turn the line into a double
input_double = Double.parseDouble(text);

// Turning it into an double succeeded!
// Leave the while loop
break;
} catch (NumberFormatException e)
{
// Turning it into an double failed.
System.out.println("***** Error: the text inserted is not a double! *****");
}
}

System.out.print("Please enter a string: ");
input_string = input.nextLine();

// This is done automatically when the program stops, but it's
// a good habit to get into for longer running programs.
input.close();
}

}

关于java - 扫描仪java验证和多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33836040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com