Java无法识别计算?

 Amy爱爸爸爱妈妈 发布于 2023-02-13 12:50

我是Java的初学者,失去了情节.有人可以帮助我,我只是想让这个代码工作.一切都运行良好,除了最后的计算.我复制并粘贴了代码和输出.

import java.util.Scanner;
public class newfile {
public static void main(String[]args) { // starting point of the program
    Scanner input = new Scanner(System.in);
    int oneA, oneB, oneC;
    double finA=0, finB=0, finC=0,average=0;

    System.out.println("Hello, Welcome to the 'Calculate Your Grades' program!\nPlease input your predicted grade for Assignment 1A in a percentage format:");
    oneA=input.nextInt();
    System.out.println("Thank you, can you please input your predictive grade for Assignment 1B?");
    oneB=input.nextInt();
    System.out.println("Finally, can you please input your predictive grade for Assignment 1C?");
    oneC=input.nextInt();
    average=(oneA+oneB+oneC)/3;
    System.out.println("Excellent, Thank you. The program has calculated your average grade to be "+average+"%");
    finA=(oneA/100)*25;
    finB=(oneB/100)*25;
    finC=(oneC/100)*50;
    System.out.println("This module states:\n Assignment 1A is weighted at 25%. You achieved "+finA+"%\n Assignment 1B is weighted at 25%. You achieved "+finB+"%\n Assignment 1C is weighted at 50%. You achieved "+finC+"%");
    }       
}

输出:

--------------------Configuration: newfile - JDK version 1.7.0_45  - --------------------

Hello, Welcome to the 'Calculate Your Grades' program!
Please input your predicted grade for Assignment 1A in a percentage format:
60
Thank you, can you please input your predictive grade for Assignment 1B?
60
Finally, can you please input your predictive grade for Assignment 1C?
60
Excellent, Thank you. The program has calculated your average grade to be 60.0%
This module states:
Assignment 1A is weighted at 25%. You achieved 0.0%
Assignment 1B is weighted at 25%. You achieved 0.0%
Assignment 1C is weighted at 50%. You achieved 0.0%

流程已完成.

构建报告很清楚.

1 个回答
  • average=(oneA+oneB+oneC)/3;

    同样在这里:

    finA=(oneA/100)*25;
    finB=(oneB/100)*25;
    finC=(oneC/100)*50;
    

    在计算机程序中划分整数需要特别小心.一些编程语言,处理整数除法(即通过给出整数商作为答案).所以答案是整数.

    你正在执行整数除法.因此:

    60/100 => 0 
    

    您可以通过将其中一个参数设为double来避免这种情况:

    finA=(oneA/100.0)*25;
    finB=(oneB/100.0)*25;
    finC=(oneC/100.0)*50;
    

    2023-02-13 12:53 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有