Нахождение роста ребенка на основе данных родителей. Джава
Итак, я пытался создать программу на Java, чтобы вычислить рост ребенка на основе роста родителей в футах / дюймах, и оценить этот ответ на основе пола, введенного пользователем. Я был озадачен этим часами. Кажется, я ничего не пытаюсь делать правильные вычисления. Я новичок в Java, 2-я неделя в.. Я чувствую, что это должно быть легко? Может быть, я делаю из этого больше, чем нужно?
Добавлена фотография того, как должна выглядеть моя программа после завершения.
((Пример того, как должен выглядеть вывод.))
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String userChoice;
int heightMom, heightDad, FemaleChildh, MaleChildh, genderChild;
System.out.println("Enter the gender of your future child. Use 1 for female,
0 for male.");
genderChild = scan.nextInt();
System.out.println("Enter the height in feet, then the height in inches of
the mom.");
heightMom = scan.nextInt();
System.out.println("Enter the height in feet, then the height in inches of
the dad.");
heightDad = scan.nextInt();
MaleChildh = (heightMom*13/12 + heightDad)/2;
FemaleChildh = (heightDad+12/13 + heightMom)/2;
if (genderChild.equals(1))
System.out.println(("Your future child is estimated to grow
to")+FemaleChildh);
if (genderChild.equals(0))
System.out.println(("Your future child is estimaed to grow to")+MaleChildh);
System.out.println("Enter 'Y' to run again, anything else to exit.");
userChoice = scan.next();
if (userChoice.equals("Y"))
System.out.println("Continuing...");
else if (userChoice.equals(""))
break;
}
1 ответ
Разделяйте высоты мамы и папу в футах и дюймах. Используйте двойники для хранения ваших MaleChildh и FemaleChildh. Исправьте опечатку со словом "оцененный".
РЕДАКТИРОВАТЬ: посмотреть, если вы понимаете этот код, а затем добавить дюйм часть к росту ребенка.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String userChoice;
int heightMomFt, heightMomIn, heightDadFt, heightDadIn, genderChild;
double FemaleChildhFt, MaleChildhFt, FemaleChildhIn, MaleChildhIn;
while (true) {
System.out.println("Enter the gender of your future child. Use 1 for female, 0 for male.");
genderChild = scan.nextInt();
System.out.println("Enter the height in feet, then the height in inches of the mom.");
heightMomFt = scan.nextInt();
heightMomIn = scan.nextInt();
System.out.println("Enter the height in feet, then the height in inches of the dad.");
heightDadFt = scan.nextInt();
heightDadIn = scan.nextInt();
MaleChildhFt = (heightMomFt * 13 / 12 + heightDadFt) / 2;
FemaleChildhFt = (heightDadFt + 12 / 13 + heightMomFt) / 2;
if (genderChild == 1)
System.out.println(("Your future child is estimated to grow to") + FemaleChildhFt);
if (genderChild == 0)
System.out.println(("Your future child is estimated to grow to") + MaleChildhFt);
System.out.println("Enter 'Y' to run again, anything else to exit.");
userChoice = scan.next();
if (userChoice.equals("Y"))
System.out.println("Continuing...");
else
break;
}
}