否则没有

时间:2013-10-18 21:45:17

标签: java if-statement syntax compiler-errors

我正在尝试为我的计算机编程类编写一个代码,用于周一的项目,我对Java很新,但我正在尝试编写一个程序,首先确定用户是否输入了一个数字是偶数还是奇数,然后确定数字是否为素数。我不确定我的算法是否正确,所以如果有人对我的算法或其他任何程序有任何更正,请说明,但我真正的问题是该程序拒绝编译。每当我尝试时,它都表示如果没有问题,那就是没有其他问题。这是我的命令框的链接:

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

enter image description here 这是我的代码:

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

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

5 个答案:

答案 0 :(得分: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.");
}

答案 1 :(得分:3)

这是问题

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

在循环之后,而不是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.");
                }
            }

哪个功能正常,但不是正确的alghoritm,你可能想要这个:

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

答案 2 :(得分:0)

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

之前没有相应的'if'。

答案 3 :(得分:0)

这不适用于Java(没有if的2个)

    if (1 == 1){

    }
    else
    {

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

    }

这有效:

                if (1 == 1){

                }
                else
                {
                    if (2 == 2){

                    }
                    else //this works
                    {

                    }
                }

答案 4 :(得分:-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.");
        }
    }
}

包括所有更正。