Java 否则没有如果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19459510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:25:33  来源:igfitidea点击:

Else without if

javaif-statementsyntaxcompiler-errors

提问by user2808951

I'm trying to write a code for my computer programming class for a project due Monday, and I'm pretty new to Java, but I'm trying to write a program that will first determine if a number the user inputs is even or odd and then determine if the number is prime or not. I'm not sure if I did the algorithm right or not, so if anyone has any corrections on the program to my algorithm or anything else please say so, but my real issue is that the program is refusing to compile. Every time I try, it says it's having an else without if problem. Here's a link to my command box:

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

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

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

enter image description hereHere's my code:

在此处输入图片说明这是我的代码:

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

If anyone could help me out with this and also any problems I may have made elsewhere in the program, I'd be very grateful! Thanks.

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

采纳答案by Sam I am says Reinstate Monica

you have an elsestatement after a forloop

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

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

回答by libik

Here is the problem

这是问题所在

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

It is after for cycle, not the if statement, you have to switch it. :

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

Which is functional but not right alghoritm, you probably want this :

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

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

回答by Don Branson

The block

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

has no corresponding 'if' before it.

在它之前没有相应的“如果”。

回答by FoggyDew

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

all corrections included.

包括所有更正。

回答by live-love

This doesn't work in Java (2 elses without if)

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

    if (1 == 1){

    }
    else
    {

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

    }

This works:

这有效:

                if (1 == 1){

                }
                else
                {
                    if (2 == 2){

                    }
                    else //this works
                    {

                    }
                }