warum überspringt er sc.nextLine()?

Status
Nicht offen für weitere Antworten.

gebr

Mitglied
ich würde gerne einen Rechner programmieren, der mit der eingabe von 2 ganzzahligen Zahlen und einen Operator, den man 2 Mal falsch eingeben darf, das Ergebnis liefert....


also, hier mein versuch.. im Prinzip funktioniert das ja schon .. aber das überspringen des ersten s=sc.nextLine() stört mich schon a bissal:

Code:
import java.util.Scanner;

public class bsp5_0725238_1 {

    public static void main(String[] args) {
        int l, i;
        double e = 0, a, b;
        boolean t = false;


        char o = 0;
        Scanner sc = new Scanner(System.in);

        System.out.println("Zahl 1: ");
        a = sc.nextDouble();

        String s = sc.nextLine();



        //System.out.println(s);

        for (i = 1; i <= 3; i++) {


            System.out.println("Operator (+-*/): ");
            s = sc.nextLine();

            l = s.length();


            if (l == 1) {

                o = s.charAt(0);


                switch (o) {

                    case '+':
                        t = true;
                        break;
                    case '-':
                        t = true;
                        break;
                    case '*':
                        t = true;
                        break;
                    case '/':
                        t = true;
                        break;
                    default:
                        t = false;
                        System.out.println("falsches Zeichen; Versuch: " + (i + 1));
                }
                if (t == true) {
                    break;
                }

            } else {
                System.out.println("zu lange; Versuch: " + (i + 1));
            }



        }
        if (i <= 3) {


            System.out.println("2. Zahl: ");
            b = sc.nextDouble();


            switch (o) {
                case '+':
                    e = a + b;
                    break;
                case '-':
                    e = a - b;
                    break;
                case '*':
                    e = a * b;
                    break;
                case '/':
                    e = a / b;
                    break;
            }

        } else {
            System.out.println("Abbruch");
        }

        System.out.println(e);

    }
}
 

HoaX

Top Contributor
schau mal in die doku?

du gibst ja als erste zahl z.B. "5<return>" ein, das nextDouble() liest ja nur die nächste zahl, also 5. übrig bleibt das return, das dann von nextLine() gelesen wird.

apidoc hat gesagt.:
Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

lass dir doch "s" ausgeben und verwende als input für zahl1 den string "5 abc" ... in s steht dann " abc"
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben