2017-02-20 292 views
-1

我需要程序打印出一个声明,提供儿童成年身高的十进制格式的脚(使用模数)(例如,如果孩子的身高是5'7“显示器应该阅读“孩子的成年身高将是5.58'不正确的输出 - 模数不正确计算

import java.text.NumberFormat; 
    import java.util.Scanner; 


    public class Workshop3GenderModification { 


    public static void main(String[] args) { 

int gender; 
gender = 'M'; 
gender = 'F'; 
double cheight=0; 

Scanner input = new Scanner(System.in); 

//father height 

    System.out.print("Enter your father height in feet "); 
    int ffeet=input.nextInt(); 

System.out.print("Enter father height in inches "); 

int finches=input.nextInt(); 

//mother height 

System.out.print("Enter mother height in feet "); 
    int mfeet=input.nextInt(); 

System.out.print("Enter mother height in inches "); 

int minches=input.nextInt(); 
int mheight = mfeet * 12 + minches; 
int fheight = ffeet * 12 + finches; 

// child gender 

System.out.print("Enter M for male or F for female "); 
    gender = input.next().charAt (0); 

// male or female 

input.nextLine(); 

switch (gender){ 
    case 'M': 
    case 'm': 
     cheight =(int)((fheight * 13/12.0)+ mheight)/2; 
     break; 
    case 'F' : 
    case 'f' : 
     cheight =(int)((mheight * 12/13.0) + fheight)/2 ; 
     break; 
    default: 
     System.out.print("Invalid entry. Please enter only M,F,m or f. "); 
     break; 
} 

int cfeet= (int)cheight/12; 
int cinched= (int)cheight%12; 
double aheight=(cfeet/cinched); 

System.out.print(cfeet +"'" + cinched + "\""); 

System.out.printf(" will be the adult child's height." +" %.2f", aheight); 


    } 

} 
+0

不知道你问,但我猜你是问有关整数除法,即'1/2 == 0'没有'0.5'变化'cfeet'为双 –

+0

对不起!我对此很陌生,我甚至不知道如何制定我的问题。当我需要输出将孩子的身高转换成十进制格式的脚时,这个程序的输出给了我:(“6'2”将是成年孩子的身高3.00“)。”5.58“” – Lisa

+0

为什么'5.58' - 你如何计算这个值?你得到了什么结果? –

回答

0

我已经在你的程序修改的只有一行。而不是

double aheight=(cfeet/cinched); 

我已经加入

double aheight=(cheight/12.0); 

什么你有没有真正意义,如果你想提供英尺和英寸为十进制值。

import java.util.Scanner; 


public class Workshop3GenderModification { 


    public static void main(String[] args) { 

     int gender; 
     gender = 'M'; 
     gender = 'F'; 
     double cheight=0; 

     Scanner input = new Scanner(System.in); 

     //father height 

     System.out.print("Enter your father height in feet "); 
     int ffeet=input.nextInt(); 

     System.out.print("Enter father height in inches "); 

     int finches=input.nextInt(); 

     //mother height 

     System.out.print("Enter mother height in feet "); 
     int mfeet=input.nextInt(); 

     System.out.print("Enter mother height in inches "); 

     int minches=input.nextInt(); 
     int mheight = mfeet * 12 + minches; 
     int fheight = ffeet * 12 + finches; 

     // child gender 

     System.out.print("Enter M for male or F for female "); 
     gender = input.next().charAt (0); 

     // male or female 

     input.nextLine(); 

     switch (gender){ 
      case 'M': 
      case 'm': 
       cheight =(int)((fheight * 13/12.0)+ mheight)/2; 
       break; 
      case 'F' : 
      case 'f' : 
       cheight =(int)((mheight * 12/13.0) + fheight)/2 ; 
       break; 
      default: 
       System.out.print("Invalid entry. Please enter only M,F,m or f. "); 
       break; 
     } 

     int cfeet= (int)cheight/12; 
     int cinched= (int)cheight%12; 
     double aheight=(cheight/12.0); 

     System.out.print(cfeet +"'" + cinched + "\""); 

     System.out.printf(" will be the adult child's height." +" %.2f", aheight); 


    } 

} 
+0

非常感谢你的协助! – Lisa

+0

不客气。 –