you have an else
statement after a for
loop
for(int i=3;i*i<=n;i+=2) { if(n%1==0) { System.out.println("Your number isn't prime."); } } else { System.out.println("Your number is prime!"); }
you probably have to make a Boolean variable to do this one. there are various ways to do it, but here’s one that I would probably use
boolean isPrime = true; for(int i=3;i*i<=n;i+=2) { if(n%1==0) { isPrime = false; } } if(isPrime) { System.out.println("Your number is prime!"); } else { System.out.println("Your number isn't prime."); }
Related Posts:
- && (AND) and || (OR) in IF statements
- Java compile error: “reached end of file while parsing }”
- Non-static variable cannot be referenced from a static context
- Non-static variable cannot be referenced from a static context
- Non-static variable cannot be referenced from a static context
- The difference between += and =+
- Error: Could not find or load main class in intelliJ IDE
- Required: Variable Found: Value
- Error: ‘else’ without ‘if’
- Short form for Java if statement
- What does “error: ‘.class’ expected” mean and how do I fix it
- Syntax error on token(s), misplaced construct(s)
- JAVA Variable declaration not allowed here
- How to resolve “Syntax error on token “else”” in Java?
- Java error: void cannot be converted to String
- Fatal error compiling: tools.jar not found:
- break statement in “if else” – java
- Best way to “negate” an instanceof
- Syntax error on token “;”, { expected after this token in Random string creator
- Why is System.out.print() not working?
- Error: ‘)’ expected compiler error
- java attempting to assign weaker access privilege error
- How to get rows and columns count of a 2D array in Java?
- How to resolve “Syntax error on token “else”” in Java?
- Is there a way to end the program if statement is false?
- What is the difference between run-time error and compiler error?
- intellij idea – Error: java: invalid source release 1.9
- String index out of bounds exception java
- How do I “decompile” Java class files? [closed]
- How do I compare strings in Java?
- Could not reserve enough space for object heap
- What does “Could not find or load main class” mean?
- What is null in Java?
- What is an illegal reflective access?
- Convert Set to List without creating new List
- Can I catch multiple Java exceptions in the same catch clause?
- What is the use of printStackTrace() method in Java?
- How do I limit the number of decimals printed for a double?
- Please initialize the log4j system properly warning
- how to iterate in List
- > in java and set their values as we do in a normal int a[i][j] matrix type [duplicate]
- Java – Best way to print 2D array?
- How to update a value, given a key in a hashmap?
- How to schedule a periodic task in Java?
- Unrecognized SSL message, plaintext connection? Exception
- What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
- How to create an empty array?
- How to decompile a whole Jar file?
- How to fix org.hibernate.LazyInitializationException – could not initialize proxy – no Session
- Find how many connected groups of nodes in a given adjacency matrix
- Java dynamic array sizes?
- Java – Best way to print 2D array?
- “cannot find symbol: method” but the method is declared
- No enclosing instance of type is accessible.
- How to remove single character from a String
- How do I set the proxy to be used by the JVM
- Where to get the tools.jar to use with the Java 8 jdk early release
- int cannot be converted to string?
- What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do
- How to fix the Hibernate “object references an unsaved transient instance – save the transient instance before flushing” error
- InvocationTargetException when running a javafx program
- PersistentObjectException: detached entity passed to persist thrown by JPA and Hibernate
- How do I use the JAVA_OPTS environment variable?
- What is the difference between iterator and iterable and how to use them?
- Netbeans installation doesn’t find JDK
- javax.xml.bind.UnmarshalException: unexpected element (uri:””, local:”Group”)
- Enqueue, Dequeue and ViewQueue in Java
- org.xml.sax.SAXParseException: Content is not allowed in prolog
- Running Command Line in Java
- Working Soap client example
- Java GUI: about getContentPane( ) method and content
- How to convert an Instant to a date format?
- equivalent to push() or pop() for arrays?
- JaxbRepresentation gives error “doesnt contain ObjectFactory.class or jaxb.index”
- How do I fix a NoSuchMethodError?
- What is the Java equivalent for LINQ?
- “Expression expected” and “Cannot find symbol” error
- Android Studio ERROR: Cannot resolve symbol ‘View’
- Static Initialization Blocks
- “Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar” when starting the Scala interpreter
- Swing/Java: How to use the getText and setText string properly
- Get keys from HashMap in Java
- Java generating non-repeating random numbers
- java eclipse red exclamation mark
- Java FileOutputStream Create File if not exists
- Configure active profile in SpringBoot via Maven
- Is there any way to program Java on Visual Studio 2015
- Keylistener not working for JPanel
- the getSource() and getActionCommand()
- Failed to install android-sdk: “java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema”
- Maven build Compilation error : Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven
- Regex pattern including all special characters
- Exception is never thrown in body of corresponding try statement
- Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.7.1 from/to central (http://repo1.maven.org/maven2)
- Using while loop as input validation [duplicate]
- java -version and javac -version showing different versions
- when spring boot startup,throw out the “method names must be tokens” exception
- How to get javax.comm API?
- how to convert exe file to .jar file? [duplicate]
- How to print a table of information in Java
- set background color: Android