首页 \ 问答 \ Java非素数(Java non-prime numbers)

Java非素数(Java non-prime numbers)

我想在一个区间内打印非素数并计算数量。 例如,在3到10之间,此素数的非素数是4,6,8,9。

我创建了一个数组并将非主数字放入数组中。 我可以在屏幕上打印它们但是当我试图到达数组上的每个元素时, nonPrime[0]nonPrime[1]似乎为0 。 此外,我需要足够的维数组,因为它无助于计算非素数的数量。

这就是我尝试过的:

public static void main (String[] args)
{       
    int x=10;//end of the interval
    int y=2;//first of the interval
    int[]nonPrime=new int[10];
    for(int i=y+1;i<x;i++)
    {
        for(int j=2;j<x;j++)
        {  
            if(i!=j)
            {
                if((i%j==0))
                {
                   nonPrime[j]=i;
                   break;
                }
            }
        }
    }
}

I want to print non-prime numbers in an interval and calculate the quantity of numbers. For example, between 3 and 10, the non-prime numbers are 4,6,8,9 for this interval.

I created an array and put the nonprime numbers into the array. I can print them on the screen but when I tried to reach every single element on the array nonPrime[0] and nonPrime[1] seems 0. Also I need enough dimension array because it doesn't help to calculate the quantity of non-prime numbers.

That's what I tried:

public static void main (String[] args)
{       
    int x=10;//end of the interval
    int y=2;//first of the interval
    int[]nonPrime=new int[10];
    for(int i=y+1;i<x;i++)
    {
        for(int j=2;j<x;j++)
        {  
            if(i!=j)
            {
                if((i%j==0))
                {
                   nonPrime[j]=i;
                   break;
                }
            }
        }
    }
}

原文:https://stackoverflow.com/questions/35347402
更新时间:2023-01-07 11:01

最满意答案

有趣的是,Qt如何演变成一个全罗盘框架,使得使用它的程序员相信任何有用的东西都必须以字母Q开头。非常有点网络。 Qt只是一个在该语言之上运行的类库,它不排除使用日常库来完成工作。 特别是当这是一个与呈现用户界面几乎没有关系的库时,Qt的工作做得很好。

有许多库可以很好地进行词法分析和解析。 首先是Lex和Yacc,Flex和Bison,等等。 你只需要Qt启用它来获取错误消息,他们很乐意支持它。


It is kinda interesting how Qt has evolved into an all-compassing framework that makes the programmer that uses it believe that anything that is useful has to start with the letter Q. Very dot-netty. Qt is just a class library that runs on top of the language, it doesn't preclude using everyday libraries that get a job done. Especially when that's a library that has little to do with presenting a user interface, the job that Qt does so well.

There are many libraries that get lexical analysis and parsing done well. That starts with Lex and Yacc, Flex and Bison next, etcetera. You only have to Qt enable it for error messages, they readily support that.

相关问答

更多
  • 主要问题 Lex(Flex)分析仪中只能有两个%%行。 ...definitions... %% ...lexical patterns... %% ...everything else... 程序Lex和Flex只是将第二个%%逐字的文件内容复制到生成的C代码中。 而C在任何时候都不喜欢%% 。 鸡蛋里挑骨头 你不应该像你想要的那样将函数嵌套在彼此中: int main(){ void showToken(char* name){ printf("<%s,%s>",name,yytext ...
  • 匹配整数的模式是: INTEGER ("\\d"), 这恰好匹配一个数字。 如果你想要不止一个,请继续 INTEGER ("\\d+"), 例如。 而且,为了完成,丢失的浮点数的其他模式可能看起来像 REAL ("(\\d+)\\.\\d+") 正如评论指出的那样。 要么 REAL ("(\\d*)\\.\\d+") 允许 0.23 太 - 如果这是你正在寻找! Your pattern to match integer is: INTEGER ("\\d"), That matches e ...
  • 一个起点是 LEX = lex .l.c: $(LEX) -t $< >$@ .c.o: $(CC) -o $@ -c $< lexy: lexical.o $(CC) -o $@ $^ -ll 这需要通过clean规则,依赖关系跟踪等进行扩展,但我认为您应该能够了解Makefile的工作原理。 A starting point would be LEX = lex .l.c: $(LEX) -t $< >$@ .c.o: ...
  • 我喜欢LLVM教程 - 首先介绍一种叫做Kaleidoscope的玩具语言的手写词法分析器,首先是一个“文字编程”练习,每个位都有很好的文本解释; 它继续构建一个以相同样式构建抽象语法树的解析器; 然后整个事情(400行手写的独立C ++ for lexer,parser和AST builder)再次显示为一个完整的.cpp源文件,因此您不必自己将它们全部放在一起。 以下章节展示了代码生成,JIT,优化......真正有用的小教程! 但是,你可以停止使用词法分析器,如果这是你现在想要了解的全部内容;-)。 ...
  • 为每个已知错误创建一个错误令牌规则,并在最后创建一个“catchall”错误令牌规则,如下所示: // valid tokens first! Number : [0-9]+; Identifier : [a-zA-Z] [a-zA-Z0-9]*; //... // "error" tokens // don't use these tokens in your grammar; They will show up as extraneous tokens during parsing and can b ...
  • 我的答案仅仅是参考Flex-Bison(或Lex-Yacc)编译模型。 我对其他模型知之甚少。 我认为词法分析器/解析器组合是同一个程序中的两个协同操作模块。 当你在Bison中使用Flex时,你会发现后者调用前者提供的函数yylex() ( yylex()相当于问题中的getNextToken()函数)。 因此,将它们视为一个程序中的合作单元而不是两个不同的程序会更有意义。 而且,如果词法分析器和解析器是两个不同的程序,则必须处理进程间通信,共享内存和相关问题,这使得手头的任务更加复杂。 要回答第二个问题 ...
  • 事实证明,我的Mac OS X Mavericks上的gcc编译器与我通过ssh在线访问的GNU gcc编译器有不同的行为。 本地编译器忽略了在线编译器生成的错误,以及具有与GNU gcc无关的本地错误。 因此,部分问题是我的困惑。 事实证明这是正确的代码: "FUNC" {RETURN(FUNC);} "START" {RETURN(START);} "PRINT" {RETURN(PRINT);} . { RETURN( yytext[0] ); } I ...
  • 词法分析器可以检测没有可能含义的字符序列(其中含义由解析器确定)。 例如,在Java中,序列bana"na不能是标识符,关键字,运算符等。 但是,词法分析器无法检测到给定的词法有效令牌是无意义的还是不合语法的。 因此,例如,Java词法分子很乐意返回标记序列final "banana" final "banana" ,分别查看关键字,字符串常量,关键字和字符串常量。 A lexer can detect sequences of characters that have no possible meanin ...
  • 有趣的是,Qt如何演变成一个全罗盘框架,使得使用它的程序员相信任何有用的东西都必须以字母Q开头。非常有点网络。 Qt只是一个在该语言之上运行的类库,它不排除使用日常库来完成工作。 特别是当这是一个与呈现用户界面几乎没有关系的库时,Qt的工作做得很好。 有许多库可以很好地进行词法分析和解析。 首先是Lex和Yacc,Flex和Bison,等等。 你只需要Qt启用它来获取错误消息,他们很乐意支持它。 It is kinda interesting how Qt has evolved into an all-c ...
  • 我会尝试看一下那些更好的源代码。 我过去使用过Sablecc 。 如果您转到此页面,描述如何设置您的环境,则可以找到指向其源代码的链接 。 Antlr也是一个非常常用的。 这是它的源代码。 此外, 龙书真的很棒。 正如SK-logic建议的那样,我将现代编译器实现添加为另一种选择。 I would try taking a look at the source code for some of the better ones out there. I have used Sablecc in the pas ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)