2017-02-20 246 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

不客氣。 –