JAVA如何允许用户使用字符串输入跳过提示的数字要求?
初始问题 我正在创建一个程序,要求用户将学生添加到一个类中。学生应该有一个id,可以有一个分数(双倍)和一个字母等级(字符串)。在下面的部分中,将提示用户输入双精度数据类型的分数。我想允许用户通过输入字符串数据类型的“s”来跳过。鉴于可变分数是双重数据类型,如何做到这一点JAVA如何允许用户使用字符串输入跳过提示的数字要求?,java,skip,Java,Skip,初始问题 我正在创建一个程序,要求用户将学生添加到一个类中。学生应该有一个id,可以有一个分数(双倍)和一个字母等级(字符串)。在下面的部分中,将提示用户输入双精度数据类型的分数。我想允许用户通过输入字符串数据类型的“s”来跳过。鉴于可变分数是双重数据类型,如何做到这一点 System.out.println("Kindly input Score: (Enter s to Skip)"); score = input.nextDouble();
System.out.println("Kindly input Score: (Enter s to Skip)");
score = input.nextDouble();
输出:请输入分数:(输入s跳过) 编辑的问题
现在多亏了你们的反馈,我成功地创建了一个字符串变量行来读取用户输入,然后检查它是否为“S”/“S”,否则将值解析为double。现在基于这个问题,如果用户决定跳过提示,我如何跳过提示并继续下一个提示? 我试着用休息;但它退出了整个循环。有没有办法跳过分数问题,继续字母分数问题
// Prompting the user for Score (Numerical Grade)
System.out.println("Kindly input Score: (Enter s to Skip)");
// reading the input into the line variable of string datatype
String line = input.nextLine();
// checking if line =="s" or =="S" to skip, otherwise
// the value is parsed into a double
if("s".equals(line) || "S".equals(line))
{
break; // this exists the loop. How can I just skip this requirement
//and go to the next prompt?
}else try
{
score = Double.parseDouble(line);
System.out.println(score);
} catch( NumberFormatException nfe)
{
}
// Prompting the user for Numerical Grade
System.out.println("Kindly input Grade: (Enter s to Skip)");
String line2 = input.nextLine();
if("s".equals(line2) || "S".equals(line2))
{
break; // this exists the loop. How can I just skip this
// requirement and go to the next prompt?
}else try
{
score = Double.parseDouble(line2);
System.out.println(score);
} catch( NumberFormatException nfe)
{
}
无法使用
nextDouble()
方法读取字符串
相反,您可以读取字符串并尝试将其解析为double:
String line = input.nextLine();
if(line.equals("s")) {
// skip the line
} else {
try {
double grade = Double.parseDouble(line);
// nice, it was a double... we can work with the grade now
} catch(NumberFormatException e) {
// neither an "s", nor a properly formatted double.
// Time for error handling.
}
}
有关更多信息,请查看
编辑:首先检查“s”,然后分析双精度。感谢@AndyTurner的提示。使用
nextDouble()
方法无法读取字符串
相反,您可以读取字符串并尝试将其解析为double:
String line = input.nextLine();
if(line.equals("s")) {
// skip the line
} else {
try {
double grade = Double.parseDouble(line);
// nice, it was a double... we can work with the grade now
} catch(NumberFormatException e) {
// neither an "s", nor a properly formatted double.
// Time for error handling.
}
}
有关更多信息,请查看
编辑:首先检查“s”,然后分析双精度。感谢@AndyTurner的提示。验证用户是否输入了“s”。如果是,请跳过它,否则请解析一个双精度:
double score;
String line = input.nextLine();
if ( "s".equals(line) ) {
// skip
} else try {
score = Double.parse(line);
} catch ( NumberFormatException nfe ) {
// it wasn't a valid double... handle here
}
要跳到下一个提示,只需反转逻辑,而不是测试行是否为“s”,而是测试行是否为而不是以读取值
// psuedo code
while( true ) {
line = "enter score or s to skip".prompt()
if (!"s".equals(line)) { // it wasn't s, read it
score = Double.parseDouble(line)
} else {
... skip or assign a default
score = 0.0d;
}
line = "enter grade or s to skip".prompt()
if ( !"s".equals(line) ) { it wasn't s, then read
grade = line;
} else {
.. skip or put default
grade = "A"
}
}验证用户是否输入了“s”。如果是,请跳过它,否则请解析一个双精度:
double score;
String line = input.nextLine();
if ( "s".equals(line) ) {
// skip
} else try {
score = Double.parse(line);
} catch ( NumberFormatException nfe ) {
// it wasn't a valid double... handle here
}
要跳到下一个提示,只需反转逻辑,而不是测试行是否为“s”,而是测试行是否为而不是以读取值
// psuedo code
while( true ) {
line = "enter score or s to skip".prompt()
if (!"s".equals(line)) { // it wasn't s, read it
score = Double.parseDouble(line)
} else {
... skip or assign a default
score = 0.0d;
}
line = "enter grade or s to skip".prompt()
if ( !"s".equals(line) ) { it wasn't s, then read
grade = line;
} else {
.. skip or put default
grade = "A"
}
}使用
nextLine()
而不是nextDouble()
;检查他们是否输入了s
,否则将其解析为double。因此分数的数据类型应保持为double?是。但是您将nextLine()
分配给临时字符串。主题外注释:将大括号放在同一行的末尾,而不是新行;这是惯例,所以你可以随时问一个新问题,它们是免费的:)使用nextLine()
而不是nextDouble()
;检查他们是否输入了s
,否则将其解析为double。因此分数的数据类型应保持为double?是。但是您将nextLine()
分配给临时字符串。主题外注释:将大括号放在同一行的末尾,而不是新行;这是惯例,所以,你总是可以问一个新问题,它们是免费的:)这很有帮助。。所以,我尝试了下面的代码,但我得到了这个错误:double不能转换为string。System.out.println(“请输入分数:(输入s以跳过)”;String line=input.nextLine();如果(行==“s”| |行==“s”){break;}或者{grade=Double.parseDouble(行)}请在问题中编辑代码。作为注释阅读是非常困难的。此外,永远不要使用=
比较两个字符串,始终使用equals()
@TimoSta异常用于处理异常情况。它们不应用于用户输入正确的内容:首先检查它是否为s
,如果不是,则对其进行分析。@TimoSta但事实是,如果用户输入有效输入,则不应出现异常,因为根据定义,这不是异常情况。这非常有用。。所以,我尝试了下面的代码,但我得到了这个错误:double不能转换为string。System.out.println(“请输入分数:(输入s以跳过)”;String line=input.nextLine();如果(行==“s”| |行==“s”){break;}或者{grade=Double.parseDouble(行)}请在问题中编辑代码。作为注释阅读是非常困难的。此外,永远不要使用=
比较两个字符串,始终使用equals()
@TimoSta异常用于处理异常情况。它们不应用于用户输入正确的内容:首先检查它是否为s
,如果不是,则对其进行分析。@TimoSta但事实是,如果用户输入有效输入,则不应出现异常,因为根据定义,这不是异常情况。这非常有用!我设法创建了一个字符串变量行来读取用户输入,然后检查它是否为“S”/“S”,否则将值解析为double。现在基于这个问题,如果用户决定跳过提示,我如何跳过提示并继续下一个提示?我试着用休息;但它退出了整个循环。有没有办法跳过分数问题,继续字母分数问题?(请检查编辑的问题)这非常有帮助!我设法创建了一个字符串变量行来读取用户输入,然后检查它是否为“S”/“S”,否则将值解析为double。现在基于这个问题,如果用户决定跳过提示,我如何跳过提示并继续下一个提示?我试着用休息;但它退出了整个循环。有没有办法跳过分数问题,继续字母分数问题?(请检查编辑的问题)