我让用户输入他们的位置。 并根据他们的位置有薪酬差异。 if语句不会改变变量“pay” 有人能帮助我吗?
import java.util.HashMap;
import javax.swing.*;
public class TimeSheet {
public static void main(String[] args) {
String loc;
String[] startday = new String[7];
String[] endday = new String[7];
int x = 0;
int y = 0;
int num = 1;
int location;
double starttime;
double endtime;
double hour = 0;
double total = 0;
double pay = 0;
double totalpay;
loc = JOptionPane.showInputDialog(null, "Enter your Work Location");
location = Integer.parseInt(loc);
if (location == 100 && location < 200 ) {
pay = 10.00;
}
else if (location == 200 && location < 300) {
pay = 7.50;
}
else if (location == 300 && location < 400) {
pay = 9.25;
}
else if (location == 400 && location < 500) {
pay = 13.50;
}
else if (location == 500 && location < 600) {
pay = 8.00;
}
HashMap<String, Double> map = new HashMap<String, Double>();
map.put("1",9.0);
map.put("2",9.5);
map.put("3",10.0);
map.put("4",10.5);
map.put("5",11.0);
map.put("6",11.5);
map.put("7",12.0);
map.put("8",12.5);
map.put("A",13.0);
map.put("B",13.5);
map.put("C",14.0);
map.put("D",14.5);
map.put("E",15.0);
map.put("F",15.5);
map.put("G",16.0);
map.put("H",16.5);
map.put("I",17.0);
while (x < 7 && y < 7) {
startday[x] = JOptionPane.showInputDialog(null, "Enter Day "+num+" 's Starting code");
endday[y] = JOptionPane.showInputDialog(null, "Enter Day "+num+" 's Ending code");
starttime = map.get(startday[x]);
endtime = map.get(endday[y]);
hour = endtime - starttime;
total = total + hour;
totalpay = total*pay;
JOptionPane.showMessageDialog(null, "total hour" + total);
JOptionPane.showMessageDialog(null, "total pay for the week is " + totalpay);
x++;
y++;
num++;
}
}
}
答案 0 :(得分:3)
您的条件如下:
else if (location == 200 && location < 300) {
翻译成英文
如果位置正好是200且小于300。
您可能想将其更改为
如果位置大于或等于200且小于300。
答案 1 :(得分:1)
这可能就是你的if-block没有按预期工作的原因:
if (location == 100 && location < 200 ) {
pay = 10.00;
}
else if (location == 200 && location < 300) {
pay = 7.50;
}
else if (location == 300 && location < 400) {
pay = 9.25;
}
else if (location == 400 && location < 500) {
pay = 13.50;
}
else if (location == 500 && location < 600) {
pay = 8.00;
}
根据您确定定位系统的确切方式,它应该是:
if (location < 100 ) {
// Do something with pay here
pay = 0.00;
}
else if (location < 200 ) {
pay = 10.00;
}
else if (location < 300) {
pay = 7.50;
}
else if (location < 400) {
pay = 9.25;
}
else if (location < 500) {
pay = 13.50;
}
else if (location < 600) {
pay = 8.00;
}
else {
// You should probably have some error-handling here
// as fail-safe default for defensive programming
}
或者
if (location == 100) {
pay = 10.00;
}
else if (location == 200) {
pay = 7.50;
}
else if (location == 300) {
pay = 9.25;
}
else if (location == 400) {
pay = 13.50;
}
else if (location == 500) {
pay = 8.00;
}
else {
// You should probably have some error-handling here
// as fail-safe default for defensive programming
}
答案 2 :(得分:1)
一个简单的程序员101错误 - 您在使用OR时使用了AND。任何不是100,200,300,400,500的值都将失败,因为您希望它为100且<200等。 见下文:
if (location == 100 || location < 200 ) {
pay = 10.00;
}
else if (location == 200 || location < 300) {
pay = 7.50;
}
else if (location == 300 || location < 400) {
pay = 9.25;
}
else if (location == 400 || location < 500) {
pay = 13.50;
}
else if (location == 500 || location < 600) {
pay = 8.00;
}
答案 3 :(得分:0)
您的if
条件看起来很奇怪。您的意思是说>=
而不是==
吗?
if (location == 100 && location < 200 ) {
^^ should this be >= ?
顺便说一句,这些检查中有一半是不必要的:
if (location < 100) {
}
else if (location < 200 ) {
pay = 10.00;
}
else if (location < 300) {
pay = 7.50;
}
else if (location < 400) {
pay = 9.25;
}
else if (location < 500) {
pay = 13.50;
}
else if (location < 600) {
pay = 8.00;
}
答案 4 :(得分:0)
所有if语句都包含&amp;&amp;如下所示:(位置== 100 &amp;&amp; 位置&lt; 200)。这些应该替换为 || 。
&安培;&安培; = AND运算符
|| = OR运算符
请参阅以下更改:
if (location == 100 || location < 200 ) {
pay = 10.00;
}
else if (location == 200 || location < 300) {
pay = 7.50;
}
else if (location == 300 || location < 400) {
pay = 9.25;
}
else if (location == 400 || location < 500) {
pay = 13.50;
}
else if (location == 500 || location < 600) {
pay = 8.00;
}
答案 5 :(得分:0)
你真的不需要if-else乱七八糟的东西。地点图会清理它很多。
Map<String, Double> locMap = new HashMap<String, Double>();
locMap.put("100", 7.77);
locMap.put("500", 5.55);
pay = locMap.get(locStringFromInput);