问题
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++;
}
}
}
回答1:
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
if (time1.equals("1")) {
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;
}
..........
回答2:
Use this approach for static map using ENUM
public enum TimeToClock{
1(9),
2(9.5);
//so on
public final double value;
private static final Map<Integer,TimeToClock> map;
public static final TimeToClock[] VALUES = values();
static {
map = new HashMap<Integer,TimeToClock>(3);
for (TimeToClock type : VALUES) {
map.put(type.value, type);
}
}
TimeToClock(int value){
this.value = value;
}
public double getValue(){
return this.value;
}
public static TimeToClock get(int key){
return map.get(key);
}
}
Use:
double value = TimeToClock.get(2).getValue();
回答3:
Your total at the end looks like it's worked out incorrectly.
total = hour + hour;
Would it not make more sense to be...
total = total + hour;
So you are incrementing the total rather than just resetting it after each loop iteration?
来源:https://stackoverflow.com/questions/13685434/total-not-adding-up