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

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

錯(cuò)誤:- Scanner 類要求無限次輸入

錯(cuò)誤:- Scanner 類要求無限次輸入

汪汪一只貓 2021-12-10 10:26:13
import java.util.*;public class Composite_Magic{    public static void main()    {        int i,j,m,n,fact=0,sum=0,temp=0;        boolean k=false;        Scanner sc=new Scanner(System.in);        System.out.println("Enter 2 numbers as upper and lower bound and all composite numbers between them will be displayed");        m=sc.nextInt();        n=sc.nextInt();        sc.close();        if(m<n){            for(i=m;i<=n;i++)            {                for(j=1;j<=i;j++)                {                    if(i%j==0)                        fact++;                }                                    sum=i;                while(k==false)                {                    temp=sum;                    while(temp>0)                    {                        sum=sum+(temp%10);                        temp=temp/10;                    }                    if(sum/10==0)                        k=true;                }                if(sum==1 && fact>2)                     System.out.println(i);            }        }        else            System.out.println("Invalid Input");    }}所以我只要求輸入兩次,但它并沒有停止。這是我犯的錯(cuò)誤還是錯(cuò)誤?這是我的完整程序。
查看完整描述

2 回答

?
一只斗牛犬

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

只是這個(gè)循環(huán):


        while(k==false)

        {

            temp=sum;

            while(temp>0)

            {

                sum=sum+(temp%10);

                temp=temp/10;

            }

            if(sum/10==0)

                k=true;

        }

似乎永遠(yuǎn)不會(huì)結(jié)束。

我不知道你想用它做什么,但k不會(huì)變成true

,否則會(huì)花費(fèi)很多時(shí)間。

在此期間,您認(rèn)為系統(tǒng)會(huì)提示您提供新號碼,但您沒有。

您只需輸入并按回車鍵即可。

要證明這一點(diǎn),只需鍵入ppp. 這應(yīng)該拋出InputMismatchException但它沒有。


查看完整回答
反對 回復(fù) 2021-12-10
?
尚方寶劍之說

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

您必須使用關(guān)閉掃描儀,sc.close(); 但您的循環(huán)仍然存在問題,我已經(jīng)使用我自己的代碼重新植入了代碼,現(xiàn)在應(yīng)該可以使用了。


public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter 2 numbers as upper and lower bound and all composite numbers between them will be displayed");

    int m = sc.nextInt();

    int n = sc.nextInt();

    sc.close();

    if (m < n) {

        for (int i = m; i <= n; i++) {

            int f = 0;

            for (int i2 = 1; i2 <= n; i2++) {

                if (n % i2 == 0)

                    f++;

            }

            if (f > 2) {

                int num = i;

                do {

                    num = sumOfDigits(num);

                } while (num > 9);

                if (num == 1) {

                    System.out.println(i);

                }

            }

        }

    } else {

        System.out.println("Invalid Input");

    }

}


public static int sumOfDigits(int n) {

    int s = 0;

    while (n > 0) {

        s += n % 10;

        n /= 10;

    }

    return s;

}

這使得輸出


10

19

28

37

46

55

64

73

82

91

100


查看完整回答
反對 回復(fù) 2021-12-10
  • 2 回答
  • 0 關(guān)注
  • 210 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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