Logic Error in Java Program -
is writing a program that reads numbers from a text file and checks whether they are prominent or not. The text file contains the following numbers, with 98, 76, 84, 69, 92, 83, 88, 90, 72, 66. The first number is not 98, but the second number (76) should come in the form of a prime minister. My printed results do not show all numbers that are not true.
Import java.io. *; Import java.util.Scanner; Public class AssignFive_FileRead {public static zero main (string [] args throws IOException {int number; Int calc = 0; Int i = 2; File myFile = new file ("Numbers.txt"); Scanner inputfile = new scanner (mimefile); // Check if the file exists (! MyFile.exists ()) {System.out.println ("Error: File can not be found"); System.exit (0); } Else {System.out.println ("The file is found, the operation is starting ..."); } // while reading points from the text file (inputFile.hasNext ()) {number = inputFile.nextInt (); // Calculate that the number is cardinal (i & lt; = number / 2), then start to see {if (number% i == 0) {calc = 1; } I ++; } // while looping on the other side (call == 1) {System.out.println ("number:" + number + "is not prime"); } Else {System.out.println ("number:" + number + "is prime!"); }} // Finally the first loop} // and main} // and the public class
There is a bug in your code (I see it).
But the major error is in your testing method.
You say that 76 is not a major number. 76 is 38x2 , and this means that it is not the principal. (In fact, any positive number that is greater than 2 and greater than ...) In fact, 83 is the only major number in that list.
Comments
Post a Comment