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

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

試圖打印出 100 個奇數(shù)

試圖打印出 100 個奇數(shù)

達(dá)令說 2022-07-14 10:17:46
import java.util.*;public class lab7 {public static void isPrime(int n,boolean isPrime){for (int div = 2; div < n; div++) {        if (n % div == 0) { // n is not prime           isPrime = false;           div = n;        }else{     isPrime=true;     }     }        }   // This program prints out the first 100 prime numberspublic static void main(String[] args) {  int count = 0;  int n = 1;  boolean isPrime=true;  // loop that iterates 100 times  while (count <= 100) {     // Use the isPrime method to check whether     // the number n is prime or not     if (isPrime(n)) {        System.out.println(n + " is prime");        count++;     }     // move on to the next n     n++;  } }}我試圖讓代碼使用一種名為 isPrime 的方法打印出前 100 個奇數(shù)。我不斷收到錯誤消息    lab7.java:35: error: method isPrime in class lab7 cannot be applied to given types;     if (isPrime(n)) {         ^required: int,booleanfound: int我將如何擺脫它并做我想做的事。
查看完整描述

4 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個贊

您的isPrime(int)函數(shù)應(yīng)如下所示:


public static boolean isPrime(int n) {

 for (int div = 2; div < n; div++) {

  if (n % div == 0) { // n is not prime

   return false;

  } else {

   return true;

  }

 }


 return false;

}

您的實(shí)施不起作用,因?yàn)椋?/p>

  1. 你沒有boolean從函數(shù)中返回

  2. Java 是按值傳遞的,因此您的變量isPrime不會更新

還要確保不要混淆素數(shù)和奇數(shù)之間的差異。


查看完整回答
反對 回復(fù) 2022-07-14
?
瀟瀟雨雨

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

您的實(shí)施不起作用,因?yàn)椋?/p>


你沒有boolean從函數(shù)中返回

Java 是按值傳遞的,因此您的變量isPrime不會更新

還要確保不要混淆素數(shù)和奇數(shù)之間的差異。


查看完整回答
反對 回復(fù) 2022-07-14
?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個贊

public class lab7 {

public static boolean isPrime(int n,boolean isPrime){

for (int div = 2; div < n; div++) {

        if (n % div == 0) { // n is not prime

           isPrime = false;

           div = n;

        }else{

     isPrime=true;

     }


     }

return isPrime;

     }


   // This program prints out the first 100 prime numbers


public static void main(String[] args) {

  int count = 0;

  int n = 1;

  boolean isPrime=true;


  // loop that iterates 100 times

  while (count <= 100) {


     // Use the isPrime method to check whether

     // the number n is prime or not

     if (isPrime(n, isPrime)) {

        System.out.println(n + " is prime");

        count++;

     }


     // move on to the next n

     n++;

  }

 }

}

我已經(jīng)更正了代碼。請檢查。


查看完整回答
反對 回復(fù) 2022-07-14
?
侃侃爾雅

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個贊

我看到的問題是 isPrime 需要兩個變量,在第 35 行的代碼中,您只提供了 1,即整數(shù)。



查看完整回答
反對 回復(fù) 2022-07-14
  • 4 回答
  • 0 關(guān)注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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