如果值不正确,我应该如何使代码给出错误

发布于 2025-01-13 12:52:48 字数 1648 浏览 2 评论 0原文

我不知道怎么做,但我必须添加一些东西,如果它是负值或字符,则输入错误

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔芯 2025-01-20 12:52:48

你可以这样做 - 如果任何值无效,它将再次接受输入而不是抛出。

 public static void main(String[] args) {
 
 
 boolean wrongAge = false;
 boolean wrongGender = false;
 boolean wrongBP = false;
 
 Scanner scn = null;
 
 double age = 0;
 double gender = 0;
 double bp = 0;
 
 do{
     try{
        scn=new Scanner(System.in);
        System.out.print("type in the age: ");
        wrongAge = false;
        age=scn.nextDouble();        
     }catch(InputMismatchException ime){
         wrongAge = true;
     }
 }while(wrongAge);
 
  do{
     try{
        scn=new Scanner(System.in);
        System.out.print("select your gender (0: female, 1: male):");
        wrongGender = false;
        gender=scn.nextDouble();        
     }catch(InputMismatchException ime){
         wrongGender = true;
     }
 }while(wrongGender);
 
  do{
     try{
        scn=new Scanner(System.in);
         System.out.print("entere your bp: ");
        wrongBP = false;
        bp=scn.nextDouble();        
     }catch(InputMismatchException ime){
         wrongBP = true;
     }
 }while(wrongBP);
 

    if(age>0){
     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");
         }
     }
     }
    }else{
        System.out.print("Incorect age !!!");
    }    
    }

you can do in this way - If any value will be invalid it will take input again rather then throwing.

 public static void main(String[] args) {
 
 
 boolean wrongAge = false;
 boolean wrongGender = false;
 boolean wrongBP = false;
 
 Scanner scn = null;
 
 double age = 0;
 double gender = 0;
 double bp = 0;
 
 do{
     try{
        scn=new Scanner(System.in);
        System.out.print("type in the age: ");
        wrongAge = false;
        age=scn.nextDouble();        
     }catch(InputMismatchException ime){
         wrongAge = true;
     }
 }while(wrongAge);
 
  do{
     try{
        scn=new Scanner(System.in);
        System.out.print("select your gender (0: female, 1: male):");
        wrongGender = false;
        gender=scn.nextDouble();        
     }catch(InputMismatchException ime){
         wrongGender = true;
     }
 }while(wrongGender);
 
  do{
     try{
        scn=new Scanner(System.in);
         System.out.print("entere your bp: ");
        wrongBP = false;
        bp=scn.nextDouble();        
     }catch(InputMismatchException ime){
         wrongBP = true;
     }
 }while(wrongBP);
 

    if(age>0){
     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");
         }
     }
     }
    }else{
        System.out.print("Incorect age !!!");
    }    
    }
执妄 2025-01-20 12:52:48

最简单的方法是在代码的任何位置抛出 RunTimeException,如下所示。

throw new RuntimeException("your message here.");

The easiest way is to throw a RunTimeException at any position of your code, like below.

throw new RuntimeException("your message here.");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文