사용자가 잘못된 체중이나 나이를 입력하면 오류 메시지가 즉시 표시되고 프로그램이 종료됩니다. 나이와 체중이 모두 입력되면 내 프로그램에 오류 메시지가 표시됩니다. 누구든지 나를 도울 수 있습니까? 예 : switch 문에 1을 입력하여 월 단위로 나이를 입력하도록 선택하고 kg 단위의 가중치를 입력합니다. 사용자가 해당 달에 -5를 입력하면 즉시 오류 메시지를 표시하고 프로그램을 종료해야합니다. 내 프로그램이하는 일은 나이가 들어서 -5를 입력 한 후 사용자가 체중을 입력하라는 메시지를 표시합니다. 무게가 입력 된 후 오류 메시지가 나타나고 프로그램이 끝납니다. 어떤 도움을 많이 주시면 감사하겠습니다. 당신이 달을 입력 한 후여러 if 문이있는 즉시 오류 메시지가 표시됩니다. 내 프로그램에 모두 입력 한 후에 오류 메시지가 표시됩니다.
import java.util.Scanner;
import java.lang.Math;
public class infant{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number;
double ageMonths = 0;
double ageYears = 0;
double weightPounds = 0;
double weightKg = 0;
System.out.println("Choice 1 input the age in months, then weight in kilograms"); // this tells the user what the numbers 1-4 does when chosen
System.out.println("Choice 2 input the age in years, weight in kilograms");
System.out.println("Choice 3 input the age in months, weight in pounds");
System.out.println("Choice 4 input the age in years, weight in pounds");
number =sc.nextInt();
if(number<=4 && number>=1){ // if statement that makes the user enter a value from 1 to 4 for the switch statment.
System.out.println("valid number selection");
}else{ System.out.println("need to choose a number between 1 and 4");
return;
}
switch(number){ // switch statment that allows the user to choose how they want to enter the height and weight
case 1: System.out.println("age in months: "); ageMonths =sc.nextDouble();
System.out.println("weight in kilograms: "); weightKg =sc.nextDouble();
ageYears = ageMonths * 0.0833334; // this switch statment also converts Kg to pounds and years to months in order for math equation in the if statement at then end of the program to work.
weightPounds = weightKg * 2.20462;
break;
case 2: System.out.println("age in years: "); ageYears =sc.nextDouble();
System.out.println("weight in kilograms: "); weightKg =sc.nextDouble();
ageMonths = ageYears * 12;
weightPounds = weightKg * 2.20462;
break;
case 3: System.out.println("age in months: "); ageMonths =sc.nextDouble();
System.out.println("weight in pounds: "); weightPounds =sc.nextDouble();
ageYears = ageMonths * 0.0833334;
weightKg = weightPounds * 0.453592;
break;
case 4: System.out.println("age in years: "); ageYears =sc.nextDouble();
System.out.println("weight in pounds: "); weightPounds =sc.nextDouble();
ageMonths = ageYears * 12;
weightKg = weightPounds * 0.453592;
break;
default: System.out.println("you entered in an invalid number");
}
if(ageMonths<=24 && ageMonths>-0.1){ // if statement that makes sure the ages are in the correct range
System.out.println("valid age input");
}else if(ageYears>-0.1 && ageYears<=2){
System.out.println("valid age input");
}else{ System.out.println("You entered a invaild age 0 to 24 months only");
return;
}
if(weightPounds>=4.40925 && weightPounds<=33.0693 || weightKg>=2 && weightKg<=15){ // if statement that makes sure the weight are in the correct range
System.out.println("valid weight input");
}else{ System.out.println("You entered a invaild weight 2kg to 15kg only");
return;
}
if(weightKg >= ageYears * ageMonths/3 + 2 && weightKg <= 5 * ageYears * ageMonths/12 + 5){ // if statement that does a math equation to determine if your infant is healthy or not
System.out.println(" your infant is healthy");
}else{ System.out.println(" your infant is not healthy");
}
}
}
기본적으로 귀하의 말은 내 스위치 명세서의 무게와 나이에 대해 if 문을 사용해야한다는 것입니다. –
그래, 기본적으로. 하지만 모든 경우에 검사 부분을 복사 할 필요가 없습니다. 이는 함수를 구성 할 수 있기 때문입니다. –
코드 함수 checkWeights (double weight)를 사용하려고했습니다. { –