输出错误 - 模数计算不正确

时间:2017-02-20 04:25:43

标签: java modulus

我需要程序打印出一个声明,以十进制格式的英尺(使用模数)提供儿童成人身高(如果孩子的身高是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);


   }

}

1 个答案:

答案 0 :(得分: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);


    }

}