简体   繁体   中英

New | Double output Using Scanner class in for loop with nested if statements

I am for one very new to coding and needed some help and I really am stuck and looked around but couldn't find exactly what I was doing wrong so the search was impossible.

So I have a lab where you are asking the user for an integer but , after the first integer is read I have to ask the user(using a string or char) to ask if they would like to enter another integer. If the user enters 'N' it will skip and end the program. If the user enters a 'Y' they will enter another integer and it will keep track of whether the integer is even or odd and so on and so on until the user enters a 'N' and display at the end the total number of even or odd numbers entered. I have attempted this in a unique way I think, most likely not in a very 'neat' way. Output usually ends up like: Would you like to enter an integer?: yes Enter an integer: 7 7 is Odd! Do you have another integer to enter (Y/N)?: Y Do you have another integer to enter (Y/N)?: Enter an integer: 6 6 is Even! Do you have another integer to enter (Y/N)?: N Do you have another integer to enter (Y/N)?: Odd numbers: 1 Even numbers: 1 I just keep getting that "Do you want too..." Im not to sure how to explain this I really hope I am not to vague thank you.

    String Y = " ";

    int integer = 0; 
    int even = 0;
    int odd = 0;
    int x = 0;
    int b = 0;




    System.out.println("Would you like to enter an integer?: ");
    yes = in.nextLine();

    if(yes.equals("yes") || yes.equals("yes"))
    {


                         System.out.println("Enter an integer: ");
                        integer = in.nextInt();   
                        if((integer % 2)==0||(integer ==0))
                        {
                            System.out.println(integer+ " is Even!");
                                       even++;
                        }
                        else if((integer %1)==0)
                        {
                            System.out.println(integer+ " is Odd!");
                                    odd++;
                        }
                        x = 10;


        for(int i = 5; i <= x; i++)
                {

                    Y = in.nextLine();
                    System.out.println("Do you have another integer to enter (Y/N)?: ");





                            if(Y.equals("Y"))
                            {
                                b = 1;
                                if(b == 1)
                                {
                                        x = 10;
                                        b = 0;

                                        System.out.println("Enter an integer: ");
                                        integer = in.nextInt();

                                        if((integer % 2)==0||(integer ==0))
                                         {
                                             System.out.println(integer+ " is Even!");
                                             even++;
                                         }
                                        else if((integer %1)==0)
                                        {
                                             System.out.println(integer+ " is Odd!");
                                             odd++;
                                         }
                                }

                            }
                            else if(Y.equals("N"))
                            {
                                Y = "N";
                                x = 0;
                            }
                }
            System.out.println("Odd numbers: "+odd);
            System.out.println("Even numbers: "+even);

    }





}

Let's break down your code and maybe you can see what is wrong here. If you had just read your code line by line, you would have seen most of this yourself. It appears you need to learn about loops, specifically for this task, the while loop. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

Second, to count the number of integers equaling either odd or even, use a counter. Search on this site or Google for a lot of resources.

All my comments point out some obvious flaws with the code:

 String Y = " ";

int integer = 0;
int even = 0;
int odd = 0;
int x = 0;
int b = 0;

// no main method

//Specify what the user input should be to continue or to exit
//your program in the first question, not later on. They have no idea 
//what word you are expecting them to type. What if they entered 
//"sure" or "yep" or "nah"

System.out.println("Would you like to enter an integer?: ");

//This needs to be declared.
yes =in.nextLine();

//At this point, you have no way to exit the program because you have not utilized any
//sort of loop. A while loop would work great here.

// this if statement makes no sense and is redundant

    if(yes.equals("yes")||yes.equals("yes"))

{
    System.out.println("Enter an integer: ");
    integer = in.nextInt();

    if ((integer % 2) == 0 || (integer == 0)) {
        System.out.println(integer + " is Even!");
        even++;
    } else if ((integer % 1) == 0) {
        System.out.println(integer + " is Odd!");
        odd++;
    }

    //Why is int x even here? What is its purpose?
    x = 10;

    //Again, what is this for loop for?
    for (int i = 5; i <= x; i++) {



        //This entire section doesn't need to be here. Use a loop. 
    /*
        Y = in.nextLine();
        System.out.println("Do you have another integer to enter (Y/N)?: ");


        if (Y.equals("Y")) {
            b = 1;
            if (b == 1) {
                x = 10;
                b = 0;

                System.out.println("Enter an integer: ");
                integer = in.nextInt();

                if ((integer % 2) == 0 || (integer == 0)) {
                    System.out.println(integer + " is Even!");
                    even++;
                } else if ((integer % 1) == 0) {
                    System.out.println(integer + " is Odd!");
                    odd++;
                }
            }

        } else if (Y.equals("N")) {
            Y = "N";
            x = 0;
        }
    }*/
    System.out.println("Odd numbers: " + odd);
    System.out.println("Even numbers: " + even);

}

}

Here's a simple example of what I think you're looking for:

public static void main(String[] args) {
        System.out.println("Would you like to enter an integer");
        Scanner s = new Scanner(System.in);
        String ans;
        int odd = 0, even = 0, num;
        boolean b = true;
        while (b) {
            ans = s.next();
            if (ans.equalsIgnoreCase("yes")) {
                System.out.println("Please enter an integer");
                num = s.nextInt();
                if (num % 2 == 0) {
                    even++;
                } else if (num % 1 == 0) {
                    odd++;
                }
                System.out.println("Do you have another integer to enter");
            } else {
                b = false;
            }
        }
        System.out.println("Odd: " + odd + " Even: " + even);
    }

However, you'd need to tidy it up to match exactly what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM