Java字符串比较不起作用?
2022-09-05 00:00:10
我想这很简单,但我就是不明白如何做到这一点:(到目前为止,我已经尝试了==和等于,但我都没有工作。这是一个计算瑞典高中系统成绩的小编程(所以只需忽略瑞典语并查看字符串比较。这里的问题是统计函数变量在程序结束时仍设置为0。
import javax.swing.*;
public class Uppgift1 {
private String[] subjects = {"Matematik","Svenska","Engelska","Idrott",
"Bild","Fysik","Biologi","Kemi","Historia","Geografi",
"Samhällskunskap","Religionskunskap"};`
private String[] grades = new String[subjects.length]; // Lika många element som ämnen
public void setGrades() {
for(int i = 0; i < grades.length; i++){
grades[i] = JOptionPane.showInputDialog("Ange betyg i " + subjects[i] + " (IG, G, VG, MVG)");
}
}
public void statistics() {
int ig = 0;
int g = 0;
int vg = 0;
int mvg = 0;
int points = 0;
for(int i = 0; i < grades.length; i++){
if(grades[i].equals("IG")){
ig++;
}
else if(grades[i] == "G"){
g++;
points += 10;
}
else if(grades[i] == "VG"){
vg++;
points += 15;
}
else if(grades[i] == "MVG"){
mvg++;
points += 20;
}
}
String output = new String("BETYGSSTATISTIK\n");
output += "IG: " + ig + "\n";
output += "G: " + g + "\n";
output += "VG: " + vg + "\n";
output += "MVG: " + mvg + "\n";
output += "Betygspoäng: " + points;
JOptionPane.showMessageDialog(null, output);
}
public static void main(String[] args) {
Uppgift1 prog = new Uppgift1();
prog.setGrades();
prog.statistics();
}
}