Java: 如何将字符串转为整数

Java: How to turn a string into a int

我最近刚开始用 Java(一周)编码。

我想编写一个程序,在我向控制台输入 'e' 时结束。但是,当我输入 'e' 时,我得到

Exception in thread "main" java.util.InputMismatchException

你能帮我看看哪里出了问题吗?

代码如下:

    void top() {

    String e = "0";
    int i=Integer.parseInt(e);
    
    int alternierendeSumme = 0;
    int eingegebeneZahl = eingabeZahl("Geben Sie eine ganze Zahl ein. 0 = Abbrechen.");
    int counter = 0;
    
    //Integer intobject = new Integer(0);
    //int e = intobject.intValue();
    
    while(eingegebeneZahl != i) {
        
        eingegebeneZahl = eingabeZahl("Geben Sie eine ganze Zahl ein. 0 = Abbrechen.");
        counter = counter + 1;

             if ((counter % 2) == 0) {
                 alternierendeSumme = alternierendeSumme - eingegebeneZahl;
             } else { alternierendeSumme = alternierendeSumme + eingegebeneZahl;
             }              
    }
        
System.out.println("Die alternierende Summe beträgt [" + alternierendeSumme + "]"); 

}       

void print(String text) {
    System.out.println(text);
}

Scanner sc = new Scanner(System.in);
int eingabeZahl(String text) {
    print(text);                                                
    return sc.nextInt();
}

String eingabeZahl() {
    return sc.next().toString();
}

}

为什么需要将字符串“0”转换为数字0?做类似

的事情会简单得多
while(input != 0){
    exit program
}

这样你就不必将字符串转换为 int 并且它会稍微简化你的代码

我认为你的代码工作正常,尽管我确实有一些建议(见下文)

import java.util.Scanner;

public class Test {

    // changed local variables to fields to reduce method arguments
    private final Scanner scanner = new Scanner(System.in);
    private int alternatingSum;
    private int counter;

    private void top() {
        alternatingSum = 0;
        counter = 1;
        // counter > 0 is a loop invariant, used here to iterate 'forever'
        // while(true) also works
        while (counter > 0) {
            System.out.println("Geben Sie eine ganze Zahl ein. 0 = Abbrechen.");
            int entry = scanner.nextInt();
            if (entry == 0) {
                // in this case using a break reflects the real process of 
                // data entry until a special value is entered 
                // so I don't see why not
                break;
            }
            // because counter and alternatingSum are fields now, we only need
            // to pass the entry
            updateAlternatingSum(entry);

            // it's usually a good idea to update the counter at the end of the loop
            counter = counter + 1;
        }
        System.out.printf("Die alternierende Summe beträgt [%s]%n", alternatingSum);
    }

    private void updateAlternatingSum(int eingegebeneZahl) {
        // the first entry has an odd counter (1) so the number will be added to alternierendeSumme
        if (counter % 2 == 0) {
            alternatingSum = alternatingSum - eingegebeneZahl;
        }
        else {
            alternatingSum = alternatingSum + eingegebeneZahl;
        }
    }

    // added a main method in order to run the code
    public static void main(String[] args) {
        Test test = new Test();
        test.top();
    }

}