如果值不正确,我应该如何使代码给出错误
我不知道怎么做,但我必须添加一些东西,如果它是负值或字符,则输入错误
package javaapplication6;
import java.util.Scanner;
public class JavaApplication6 {
public static void main(String[] args) {
System.out.print("type in the age: ");
Scanner scn=new Scanner(System.in);
double age=scn.nextDouble();
System.out.print("select your gender (0: female, 1: male):");
double gender=scn.nextDouble();
System.out.print("entere your bp: ");
double bp=scn.nextDouble();
if(age>45){//old male
if(gender==1){
if(bp<12){
System.out.print("bp is low for old man. ");
}
else if(bp>15){
System.out.print("bp is high for old man.");
}
else if(!(bp<12 || bp>15)){
System.out.print("the bp is normal for an old man. ");
}
else {
System.out.print("the value is incorect, please try again");
}
}
}
if(age>45){//old female
if(gender==0){
if(bp<12){
System.out.print("bp is low for old woman. ");
}
else if(bp>14){
System.out.print("bp is high for old woman.");
}
else if(!(bp<12 || bp>14)){
System.out.print("the bp is normal for an old woman. ");
}
else{
System.out.print("the value is incorect, please try again");
}
}
}
}
}
I don't know how, but I must add something that gives a massage that the input was wrong if it's a negative vale or a char
package javaapplication6;
import java.util.Scanner;
public class JavaApplication6 {
public static void main(String[] args) {
System.out.print("type in the age: ");
Scanner scn=new Scanner(System.in);
double age=scn.nextDouble();
System.out.print("select your gender (0: female, 1: male):");
double gender=scn.nextDouble();
System.out.print("entere your bp: ");
double bp=scn.nextDouble();
if(age>45){//old male
if(gender==1){
if(bp<12){
System.out.print("bp is low for old man. ");
}
else if(bp>15){
System.out.print("bp is high for old man.");
}
else if(!(bp<12 || bp>15)){
System.out.print("the bp is normal for an old man. ");
}
else {
System.out.print("the value is incorect, please try again");
}
}
}
if(age>45){//old female
if(gender==0){
if(bp<12){
System.out.print("bp is low for old woman. ");
}
else if(bp>14){
System.out.print("bp is high for old woman.");
}
else if(!(bp<12 || bp>14)){
System.out.print("the bp is normal for an old woman. ");
}
else{
System.out.print("the value is incorect, please try again");
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做 - 如果任何值无效,它将再次接受输入而不是抛出。
you can do in this way - If any value will be invalid it will take input again rather then throwing.
最简单的方法是在代码的任何位置抛出 RunTimeException,如下所示。
The easiest way is to throw a RunTimeException at any position of your code, like below.