mardi 9 mars 2021

Validation of inputs from users [closed]

i have this code that describes how a human being is designed in a very conceptual way, i want to validate info from the user in order to have a control about my stats like for example hair, x legs, x arms, etc and if the user inputs an invalid value returns to assign a value again ¿what is the best way of apply this?

import javax.swing.*;


import java.util.Scanner;
 
 
public class person {

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

    
    String name = sc.nextLine();
    
    String midname = sc.nextLine();
    
    char gender = sc.next().charAt(0);
    
    int age = sc.nextInt();
    
    float height = sc.nextFloat();
    
    float weight = sc.nextFloat();

    int arms = sc.nextInt();
    
    int legs = sc.nextInt();
    if (legs>2) {
        System.out.println("Not valid");
    
    }
    else;
    
    
    String typeblood = sc.nextLine();
    
    System.out.println("¿you have hair? Y/N: ");
    char hair = sc.next().charAt(0);
    
    
    System.out.println("Your name is: "+name+" "+midname);
    System.out.println("Your gender is: "+gender);
    System.out.println("Your age is: "+age);
    System.out.println("Your age is: "+age);
    System.out.println("Your height is: "+height);
    System.out.println("Your weight is: "+weight);
    System.out.println("You have this many arms : "+arms);
    System.out.println("You have this many legs : "+legs);
    System.out.println("Type of blood: "+typeblood);
    System.out.println("You have hair? "+hair);
    
    
}

}

Aucun commentaire:

Enregistrer un commentaire