总计不加起来 [英] Total not adding up

查看:114
本文介绍了总计不加起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事高中综合课程
的课堂作业,我遇到过这个问题。
总额不添加。
有人可以告诉我原因吗?

I'm working on a classwork in high school comp sci class and i have come across this problem. the total is not adding. can someone tell me why?

import javax.swing.*;

public class TimeSheets {

  public static void main(String[] args) {

    String loc;
    String[] day = new String[7];       
    String time1;
    String time2;
    int location;
    double total = 0;
    int num = 1;
    int x = 0;
    int y = 0;
    double clock1 = 0;
    double clock2 = 0;
    double hour;

    loc = JOptionPane.showInputDialog(null, "Enter your Work Location");
    location = Integer.parseInt(loc);

    while (x < 7 && y < 7) {

      day[x] = JOptionPane.showInputDialog(null, "Enter Day "+num+" 's Starting code");
      day[y] = JOptionPane.showInputDialog(null, "Enter Day "+num+" 's Ending code");
      time1 = day[x];
      time2 = day[y];

      if (time1 == "1") {
        clock1 = 9;
      }
      if (time1 == "2") {
        clock1 = 9.5;
      }
      if (time1 == "3") {
        clock1 = 10;
      }
      if (time1 == "4") {
        clock1 = 10.5;
      }
      if (time1 == "5") {
        clock1 = 11;
      }
      if (time1 == "6") {
        clock1 = 11.5;
      }
      if (time1 == "7") {
        clock1 = 12;
      }
      if (time1 == "8") {
        clock1 = 12.5;
      }
      if (time1 == "9") {
        clock1 = 13;
      }
      if (time1 == "A") {
        clock1 = 13.5;
      }
      if (time1 == "B") {
        clock1 = 14;
      }
      if (time1 == "C") {
        clock1 = 14.5;
      }
      if (time1 == "D") {
        clock1 = 15;
      }
      if (time1 == "E") {
        clock1 = 15.5;
      }
      if (time1 == "F") {
        clock1 = 16;
      }
      if (time1 == "G") {
        clock1 = 16.5;
      }
      if (time1 == "H") {
        clock1 = 17;
      }

      if (time2 == "1") {
        clock2 = 9;
      }
      if (time2 == "2") {
        clock2 = 9.5;
      }
      if (time2 == "3") {
        clock2 = 10;
      }
      if (time2 == "4") {
        clock2 = 10.5;
      }
      if (time2 == "5") {
        clock2 = 11;
      }
      if (time2 == "6") {
        clock2 = 11.5;
      }
      if (time2 == "7") {
        clock2 = 12;
      }
      if (time2 == "8") {
        clock2 = 12.5;
      }
      if (time2 == "9") {
        clock2 = 13;
      }
      if (time2 == "A") {
        clock2 = 13.5;
      }
      if (time2 == "B") {
        clock2 = 14;
      }
      if (time2 == "C") {
        clock2 = 14.5;
      }
      if (time2 == "D") {
        clock2 = 15;
      }
      if (time2 == "E") {
        clock2 = 15.5;
      }
      if (time2 == "F") {
        clock2 = 16;
      }
      if (time2 == "G") {
        clock2 = 16.5;
      }
      if (time2 == "H") {
        clock2 = 17;
      }

      hour = clock2 - clock1;
      total = hour + hour;

      JOptionPane.showMessageDialog(null, "total hour" + total);
      x++;
      y++;
      num++;

    }
  }
}


推荐答案

用equals()方法检查字符串相等性。 == 运算符检查两个引用变量是否引用相同的字符串对象。 equals()方法检查两个字符串是否有意义相等。

check string equality with equals() method. == operator checks if two reference variables refer to the same string object. equals() method check if two strings are meaningfully equal.

    if (time1 == "1") {

sholud be

sholud be

    if (time1.equals("1")) {

以及所有其他 if '语句。我强烈建议你使用嵌套的if if而不是一百万if语句。喜欢:

and also all your other if'statements. I strongly advise you to make use of nested-if else rather than a million if statements.Like:

if (time1.equals("1")) {
        clock1 = 9;
      }
      else if (time1.equals("2")) {
        clock1 = 9.5;
      }
    ..........

这篇关于总计不加起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆