1

我正在尝试为周一到期的一个项目的计算机编程课程编写代码,而且我对 Java 还是很陌生,但我正在尝试编写一个程序,该程序首先确定用户输入的数字是偶数还是奇数,然后确定该数是否为素数。我不确定我的算法是否正确,所以如果有人对我的算法或其他任何程序有任何更正,请说出来,但我真正的问题是程序拒绝编译。每次我尝试时,它都说它有一个没有 if 问题的 else。这是我的命令框的链接:

http://s1341.photobucket.com/user/Emi_Nightshade/media/Capture_zps45f9a2ea.png.html

在此处输入图像描述 这是我的代码:

import java.io.*;
import java.util.*;

public class Lesson9p1_ThuotteEmily
{
    public static void main(String args[])
    {
        Scanner kbReader0=new Scanner(System.in);
        System.out.print("\n\nPlease enter an integer. An integer is whole number, and it can be either negative or positive. Please enter your number: ");
        long num=kbReader0.nextLong();

        if(num%2==0)                                     //if and else with braces
        {
           System.out.println("Your integer " + num + " is even.");
        }
        else
        {
            System.out.println("Your integer " + num + " is odd.");
        }

        Scanner kbReader1=new Scanner(System.in);
        System.out.print("\n\nWould you like to know if your number is prime? Please enter yes or no: ");
        String yn=kbReader1.nextLine();

        if(yn.equals.IgnoreCase("Yes"))
        {
            System.out.println("Okay. Give me a moment.");

            {
                if(num%2==0)
                {
                    System.out.println("Your number isn't prime.");
                }
                else if(num==2)
                {
                    System.out.println("Your number is 2, which is the only even prime number in existence. Cool, right?");
                }
                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!");
                }
            }
        }
        if(yn.equals.IgnoreCase("No"))
        {
            System.out.println("Okay.");
        }
    }
}

如果有人可以帮助我解决这个问题以及我在程序其他地方可能遇到的任何问题,我将不胜感激!谢谢。

4

5 回答 5

7

你有一个循环else后的语句for

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!");
}

您可能必须创建一个布尔变量来执行此操作。有多种方法可以做到这一点,但这是我可能会使用的一种

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.");
}
于 2013-10-18T21:47:24.157 回答
3

这是问题

            else
            {
                System.out.println("Your number is prime!");
            }

它在 for 循环之后,而不是 if 语句,你必须切换它。:

           if(num%2==0)
            {
                System.out.println("Your number isn't prime.");
            }
            else if(num==2)
            {
                System.out.println("Your number is 2, which is the only even prime number in existence. Cool, right?");
            }
            else
            {
                System.out.println("Your number is prime!");
            }
            for(int i=3;i*i<=n;i+=2)
            {
                if(n%1==0)
                {
                    System.out.println("Your number isn't prime.");
                }
            }

这是功能但不是正确的算法,你可能想要这个:

           if(num%2==0)
            {
                System.out.println("Your number isn't prime.");
            }
            else if(num==2)
            {
                System.out.println("Your number is 2, which is the only even prime number in existence. Cool, right?");
            }

            boolean isPrime = true;
            for(int i=3;i*i<=n;i+=2)
            {
                if(n%i==0)
                {
                    System.out.println("Your number isn't prime.");
                    isPrime = false;
                }
            }
            if (isPrime){
                System.out.println("Your number is prime!");
            }   
于 2013-10-18T21:47:38.690 回答
0

else
{
    System.out.println("Your number is prime!");
}

在它之前没有对应的'if'。

于 2013-10-18T21:48:01.790 回答
0

这在 Java 中不起作用(没有 if 的 2 个 else)

    if (1 == 1){

    }
    else
    {

    }
    else //syntax error here - else without if
    {

    }

这有效:

                if (1 == 1){

                }
                else
                {
                    if (2 == 2){

                    }
                    else //this works
                    {

                    }
                }
于 2016-01-10T02:57:04.050 回答
-1
import java.io.*;
import java.util.*;

public class Lesson9p1_ThuotteEmily {
    public static void main(String args[]) {
        Scanner kbReader0 = new Scanner(System.in);
        System.out.print("\n\nPlease enter an integer. An integer is whole number, and it can be either negative or positive. Please enter your number: ");
        long num = kbReader0.nextLong();

        if (num % 2 == 0) // if and else with braces
        {
            System.out.println("Your integer " + num + " is even.");
        } else {
            System.out.println("Your integer " + num + " is odd.");
        }

        Scanner kbReader1 = new Scanner(System.in);
        System.out.print("\n\nWould you like to know if your number is prime? Please enter yes or no: ");
        String yn = kbReader1.nextLine();

        if (yn.equalsIgnoreCase("Yes")) {
            System.out.println("Okay. Give me a moment.");

            {
                if (num % 2 == 0) {
                    System.out.println("Your number isn't prime.");
                } else if (num == 2) {
                    System.out.println("Your number is 2, which is the only even prime number in existence. Cool, right?");
                } else
                    for (int i = 3; i * i <= num; i += 2) {
                        if (num % i == 0) {
                            System.out.println("Your number isn't prime.");
                        } else {
                            System.out.println("Your number is prime!");
                        }
                    }
            }
        }
        if (yn.equalsIgnoreCase("No")) {
            System.out.println("Okay.");
        }
    }
}

包括所有更正。

于 2013-10-18T21:58:28.463 回答