否则没有if [英] Else without if

查看:151
本文介绍了否则没有if的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为一个项目的计算机编程类编写一个代码,而且我对Java很熟悉,但我试图编写一个程序,首先确定用户输入的数字是偶数或奇数,然后确定该数是否为素数。我不知道如果我做了算法对或不,所以如果任何人有任何更正的程序对我的算法或任何其他请说,但我的真正的问题是该程序拒绝编译。每次我尝试,它说它有一个没有如果问题的。这是指向我的命令框的链接:

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:

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


这是我的代码:

Here'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.

推荐答案

在<$ c之后有一个 else 语句$ c> for loop

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

这篇关于否则没有if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆