Vah*_*ahx 0 java string if-statement
我是Java的新手,我必须制作一个小型应用程序来计算最多5个数字的GCD.如果在输入5个数字之前输入为空,则应用程序将根据已给定的数字计算它
不幸的是我的代码似乎忽略了我的else if声明,这将确保它不会尝试将""添加到int数组中.
这是我正在努力的部分,我已经尝试过包含而不是等于但没有结果.我!input..写错了吗?当我尝试添加0时,代码运行正确,如果是,则不会执行else.但是,如果我输入""以使应用程序运行if语句的第一部分,它将在其完成之后转到else,并尝试向数组添加"",这当然会导致错误.我确定它缺少一些东西或者我不知道,但我似乎无法弄明白.
}else if(Integer.parseInt(input) != 0 || !input.equals(""));{
ggdGetallen[count] = Integer.parseInt(input);
count++;
txtGetal.selectAll();
}
Run Code Online (Sandbox Code Playgroud)
完整代码
private void txtGetalActionPerformed(java.awt.event.ActionEvent evt) {
String input = txtGetal.getText();
//Berekenen van het kleinste getal in het array
if(count > 4 || input.equals("")){
int kleinsteGetal = ggdGetallen[0];
for (int getal : ggdGetallen){
if (getal < kleinsteGetal && getal != 0){
kleinsteGetal = getal;
}
}
boolean isDividableBy;
boolean ggdFound = false;
while(!ggdFound){
for (int getal : ggdGetallen) {
if (getal != 0){
isDividableBy = (getal % kleinsteGetal == 0);
ggdFound = true;
if(!isDividableBy){
kleinsteGetal--;
ggdFound = false;
break;
}
}
}
}
lblResultaat.setText(String.format("De grootste gemene deler is %d", kleinsteGetal));
}else if(Integer.parseInt(input) != 0 || !input.equals(""));{
ggdGetallen[count] = Integer.parseInt(input);
count++;
txtGetal.selectAll();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
439 次 |
| 最近记录: |