第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

掃描儀 nextLine() 在 nextInt() 之后被跳過(guò)

掃描儀 nextLine() 在 nextInt() 之后被跳過(guò)

拉丁的傳說(shuō) 2024-01-05 10:31:51
我正在嘗試制作一個(gè)循環(huán)的計(jì)算器程序,用戶(hù)可以在其中輸入一個(gè)數(shù)字,然后輸入他希望對(duì)該數(shù)字執(zhí)行的操作,直到他輸入“=”作為運(yùn)算符。包含結(jié)果的變量在類(lèi)中初始化為零,并且應(yīng)用的默認(rèn)運(yùn)算符是“+”。import java.util.Scanner;public class main {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        Calculator c = new Calculator();        boolean flow = true;        while(flow) {            System.out.println("Number :");            int userEntry = scan.nextInt();            System.out.println("Operator :");            String operation = scan.nextLine();            switch(operation) {            case "+":                c.setOperation(Calculator.ADDITION);                break;            case "-":                c.setOperation(Calculator.SOUSTRACTION);                break;            case "*":                c.setOperation(Calculator.MULTIPLICATION);                break;            case "=":                c.getResult();                return;            default:                System.out.println("Please enter a valid operator.");            }            c.apply(userEntry);            c.getResult();        }    }}但每次我嘗試運(yùn)行該程序時(shí),我都會(huì)得到這個(gè)結(jié)果Number :4Operator :Please enter a valid operator.Number :67Operator :Please enter a valid operator.Number :該程序不允許我將輸入放入運(yùn)算符中并直接跳到下一個(gè) int 輸入。我一直在嘗試各種方法來(lái)編寫(xiě)此內(nèi)容,例如將該部分從循環(huán)中取出,但它仍然是相同的錯(cuò)誤,我看不出導(dǎo)致此問(wèn)題的原因。任何幫助將非常感激。
查看完整描述

1 回答

?
慕無(wú)忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

/** Scanning problems */

class Scanning {

    public static void main(String[] args) {

        int num;

        String txt;

        Scanner scanner = new Scanner(System.in);


        // Problem: nextLine() will read previous nextInt()

        num = scanner.nextInt();

        txt = scanner.nextLine();


        // solution #1: read full line and convert it to integer

        num = Integer.parseInt(scanner.nextLine());

        txt = scanner.nextLine();


        // solution #2: consume newline left-over

        num = scanner.nextInt();

        scanner.nextLine();

        txt = scanner.nextLine();

    }

}


查看完整回答
反對(duì) 回復(fù) 2024-01-05
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)